Anonymous avatar Anonymous committed 08b7fc9

Properties file backed propertyset

git-svn-id: http://svn.opensymphony.com/svn/propertyset/trunk@131 bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

Comments (0)

Files changed (1)

src/java/com/opensymphony/module/propertyset/file/PropertiesFilePropertySet.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.module.propertyset.file;
+
+import com.opensymphony.module.propertyset.memory.MemoryPropertySet;
+
+import com.opensymphony.util.Data;
+import com.opensymphony.util.XMLUtils;
+
+import org.w3c.dom.Document;
+
+import java.io.*;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+
+import java.util.*;
+
+
+/**
+ * Date: Mar 21, 2004
+ * Time: 4:50:09 PM
+ *
+ * @author hani
+ */
+public class PropertiesFilePropertySet extends MemoryPropertySet {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private File file;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void flush() throws IOException {
+        Iterator iter = getMap().entrySet().iterator();
+        Properties p = new Properties();
+
+        while (iter.hasNext()) {
+            Map.Entry entry = (Map.Entry) iter.next();
+            String name = (String) entry.getKey();
+            ValueEntry valueEntry = (ValueEntry) entry.getValue();
+            String value;
+
+            switch (valueEntry.getType()) {
+            case XML:
+                value = XMLUtils.print((Document) valueEntry.getValue());
+
+                break;
+
+            case PROPERTIES:
+            case OBJECT:
+
+                ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                ObjectOutputStream os = new ObjectOutputStream(bos);
+                os.writeObject(valueEntry.getValue());
+
+                byte[] data = bos.toByteArray();
+                value = new sun.misc.BASE64Encoder().encode(data);
+
+                break;
+
+            case DATE:
+                value = DateFormat.getDateTimeInstance().format((Date) valueEntry.getValue());
+
+                break;
+
+            case DATA:
+
+                if (valueEntry.getValue() instanceof byte[]) {
+                    value = new sun.misc.BASE64Encoder().encode((byte[]) valueEntry.getValue());
+                } else {
+                    value = new sun.misc.BASE64Encoder().encode(((Data) valueEntry.getValue()).getBytes());
+                }
+
+                break;
+
+            default:
+                value = valueEntry.getValue().toString();
+            }
+
+            p.put(name + "." + valueEntry.getType(), value);
+        }
+
+        p.store(new FileOutputStream(file), null);
+    }
+
+    public void init(Map config, Map args) {
+        super.init(config, args);
+        file = new File((String) args.get("file"));
+
+        if (file.exists()) {
+            try {
+                load();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public void load() throws IOException, ClassNotFoundException {
+        Properties p = new Properties();
+        p.load(new FileInputStream(file));
+
+        Iterator iter = p.entrySet().iterator();
+
+        while (iter.hasNext()) {
+            Map.Entry entry = (Map.Entry) iter.next();
+            String key = (String) entry.getKey();
+            int dot = key.lastIndexOf('.');
+            int type = Integer.parseInt(key.substring(dot + 1));
+            String name = key.substring(0, dot);
+            System.out.println("key=" + key + " type=" + type + " name=" + name);
+
+            String data = (String) entry.getValue();
+            Object value = null;
+
+            switch (type) {
+            case STRING:
+            case TEXT:
+                value = data;
+
+                break;
+
+            case INT:
+                value = new Integer(data);
+
+                break;
+
+            case LONG:
+                value = new Long(data);
+
+                break;
+
+            case DOUBLE:
+                value = new Double(data);
+
+                break;
+
+            case BOOLEAN:
+                value = new Boolean(data);
+
+                break;
+
+            case DATA:
+                value = new sun.misc.BASE64Decoder().decodeBuffer(data);
+
+                break;
+
+            case DATE:
+
+                try {
+                    value = DateFormat.getDateTimeInstance().parse(data);
+                } catch (ParseException e) {
+                    e.printStackTrace();
+                }
+
+                break;
+
+            case PROPERTIES:
+            case OBJECT:
+
+                byte[] bytes = new sun.misc.BASE64Decoder().decodeBuffer(data);
+                ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
+                ObjectInputStream is = new ObjectInputStream(bis);
+                value = is.readObject();
+
+                break;
+
+            case XML:
+
+                try {
+                    value = XMLUtils.parse(data);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+
+                break;
+
+            default:
+                throw new IOException("Unsupported type " + type);
+            }
+
+            getMap().put(name, new ValueEntry(type, value));
+        }
+    }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.