001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.platform.preferences.lastsettings;
005    
006    import graphlab.platform.attribute.AttributeListener;
007    import graphlab.platform.attribute.NotifiableAttributeSetImpl;
008    import graphlab.platform.extension.Extension;
009    import graphlab.platform.StaticUtils;
010    import graphlab.platform.parameter.Parameter;
011    import graphlab.platform.preferences.Preferences;
012    
013    import java.io.*;
014    import java.lang.reflect.Field;
015    import java.util.HashSet;
016    import java.util.Iterator;
017    import java.util.Map;
018    import java.util.prefs.BackingStoreException;
019    import java.util.prefs.InvalidPreferencesFormatException;
020    
021    /**
022     * @author Rouzbeh Ebrahimi
023     */
024    public class Settings implements AttributeListener {
025        private HashSet<Object> registeredObjects = new HashSet<Object>();
026        private java.util.prefs.Preferences builtInPrefs = java.util.prefs.Preferences.userRoot();
027        private java.util.prefs.Preferences graphPrefs = builtInPrefs.node("graph");
028        private HashSet<Class> registeredClasses = new HashSet<Class>();
029        private File file = new File("prefs");
030    
031        {
032            file.mkdir();
033        }
034    
035        public Settings() {
036            try {
037                File file = new File(this.file, "graph.xml");
038                if (!file.exists()) {
039                    saveSettings();
040                }
041                FileInputStream is = new FileInputStream(file);
042                graphPrefs.importPreferences(is);
043                is.close();
044    
045            } catch (IOException e) {
046                e.printStackTrace();
047            } catch (InvalidPreferencesFormatException e) {
048                e.printStackTrace();
049            }
050        }
051    
052    
053        public void registerSetting(Object o, String category) {
054            if (!getRegisteredClasses().contains(o.getClass())) {
055                getRegisteredClasses().add(o.getClass());
056                getRegisteredObjects().add(o);
057                loadSettings(o);
058                Preferences.categories.put(o, category);
059            }
060        }
061    
062        public void registerExtensionSettings(Extension e, String category) {
063    
064        }
065    
066        private void saveField(Field f, java.util.prefs.Preferences t, Object o) {
067            String key = f.getName();
068    //            Object value=o.getClass().getDeclaredField(f.getName()).get(o);
069    //        System.err.println("::::" + o);
070            Object value = null;
071            try {
072                value = f.get(o);
073            } catch (Exception e) {
074                System.err.println(f);
075                e.printStackTrace();
076                return;
077            }
078            if (value != null)
079                t.put(key, value.toString());
080            else
081                System.err.println("(Settings Error) null:" + f);
082    
083        }
084    
085        private ByteArrayOutputStream convertObjectToByteArray(Object value) {
086            ByteArrayOutputStream baos = new ByteArrayOutputStream();
087            ObjectOutputStream oos;
088            try {
089                oos = new ObjectOutputStream(baos);
090                oos.writeObject(value);
091                oos.close();
092            } catch (IOException e) {
093                e.printStackTrace();
094            }
095            return baos;
096        }
097    
098        private void loadField(Field f, java.util.prefs.Preferences t, Object o) {
099            String key = f.getName();
100            Object value = t.get(key, null);
101            String m = f.getType().toString();
102    
103            if (value == null)
104                return;
105    
106            Object obj = null;
107            try {
108                if (m.length() > 6 && m.substring(6, m.length()).equalsIgnoreCase("java.lang.String")) {
109                    obj = value;
110                } else if (m.length() > 6) {
111                    obj = StaticUtils.fromString(m.substring(6, m.length()), value.toString());
112    
113                }
114            } catch (Exception e) {
115                System.err.println(m);
116                e.printStackTrace();
117            }
118            try {
119                if (obj != null)
120                    f.set(o, obj);
121            } catch (Exception e) {
122                e.printStackTrace();
123            }
124    
125        }
126    
127        private void loadSettings(Object o) {
128            try {
129    
130                String objectName = o.getClass().getName();
131    
132                java.util.prefs.Preferences t = graphPrefs.node(objectName);
133                t.put("object", objectName);
134                for (Field f : o.getClass().getFields()) {
135                    UserModifiableProperty anot = f.getAnnotation(UserModifiableProperty.class);
136                    Parameter anot2 = f.getAnnotation(Parameter.class);
137                    if (anot != null || anot2 != null) {
138                        loadField(f, t, o);
139                    }
140                }
141                return;
142            }
143            catch (Exception e) {
144                e.printStackTrace();
145            }
146        }
147    
148        private NotifiableAttributeSetImpl refactorSerializables(NotifiableAttributeSetImpl x) {
149            NotifiableAttributeSetImpl y = new NotifiableAttributeSetImpl();
150            Map<String, Object> map = x.getAttrs();
151            Iterator<String> iterator = map.keySet().iterator();
152            for (; iterator.hasNext();) {
153                String key = iterator.next();
154                Object value = map.get(key);
155                if (value instanceof Serializable) {
156                    y.put(key, value);
157                }
158            }
159            return y;
160        }
161    
162        public void attributeUpdated(String name, Object oldVal, Object newVal) {
163            try {
164                oldVal.getClass().getDeclaredField(name).set(oldVal, newVal);
165            } catch (IllegalAccessException e) {
166    //            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
167            } catch (NoSuchFieldException e) {
168    //            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
169            }
170        }
171    
172    
173        public void saveSettings() {
174            for (Object o : getRegisteredObjects()) {
175                String objectName = o.getClass().getName();
176                java.util.prefs.Preferences t = graphPrefs.node(objectName);
177                t.put("object", objectName);
178                for (Field f : o.getClass().getFields()) {
179                    UserModifiableProperty anot = f.getAnnotation(UserModifiableProperty.class);
180                    Parameter anot2 = f.getAnnotation(Parameter.class);
181                    if (anot != null || anot2 != null) {
182                        saveField(f, t, o);
183                    }
184    
185                }
186            }
187            try {
188                graphPrefs.exportSubtree(new FileOutputStream(new File(file, "graph.xml")));
189    //            graphPrefs.exportNode(new FileOutputStream(new File("/graph.xml")));
190            } catch (IOException e) {
191    
192                e.printStackTrace();
193            } catch (BackingStoreException e) {
194                e.printStackTrace();
195            }
196    
197        }
198    
199        public HashSet<Object> getRegisteredObjects() {
200            return registeredObjects;
201        }
202    
203        public void setRegisteredObjects(HashSet<Object> registeredObjects) {
204            this.registeredObjects = registeredObjects;
205        }
206    
207        public HashSet<Class> getRegisteredClasses() {
208            return registeredClasses;
209        }
210    
211        public void setRegisteredClasses(HashSet<Class> registeredClasses) {
212            this.registeredClasses = registeredClasses;
213        }
214    }