View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: MappedConfiguration.java,v 1.2 2004/10/28 10:45:51 tassos Exp $
23  //
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.io.IOException;
28  import java.io.ObjectInputStream;
29  import java.io.ObjectOutputStream;
30  import java.sql.Connection;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Map.Entry;
38  
39  import net.sourceforge.blogentis.om.BaseConfigurable;
40  import net.sourceforge.blogentis.om.BaseConfigurablePeer;
41  import net.sourceforge.blogentis.om.StoredBlogPeer;
42  
43  import org.apache.commons.collections.CollectionUtils;
44  import org.apache.commons.configuration.BaseConfiguration;
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  import org.apache.torque.TorqueException;
48  import org.apache.torque.om.ObjectKey;
49  import org.apache.torque.util.Transaction;
50  
51  /***
52   * Class that keeps the configuration data of a blog in an in-memory HashMap.
53   * 
54   * @author abas
55   */
56  public class MappedConfiguration
57          extends BaseConfiguration {
58      private static final Log log = LogFactory.getLog(MappedConfiguration.class);
59  
60      protected BaseConfigurablePeer peer = null;
61      protected ObjectKey primaryKey = null;
62      protected boolean modified = false;
63  
64      protected void initFromBlog(BaseConfigurable obj)
65              throws Exception {
66          if (obj.getObjectData() == null)
67              return;
68          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
69              obj.getObjectData()));
70          Map map = (Map)ois.readObject();
71          for(Iterator i = map.entrySet().iterator(); i.hasNext();) {
72              Map.Entry e = (Entry)i.next();
73              addProperty((String)e.getKey(), e.getValue());
74          }
75          modified = false;
76      }
77  
78      public synchronized void saveTo(BaseConfigurable obj)
79              throws IOException {
80          if (obj.getPrimaryKey().compareTo(primaryKey) != 0
81              || obj.getConfigurablePeer() != peer) {
82              throw new IllegalArgumentException("Attempt to save to different object");
83          }
84          if (!modified)
85              return;
86          Map m = new HashMap();
87          for(Iterator i = getKeys(); i.hasNext();) {
88              String key = (String)i.next();
89              Object o = getProperty(key);
90              m.put(key, o);
91          }
92          ByteArrayOutputStream bos = new ByteArrayOutputStream(500);
93          ObjectOutputStream oos = new ObjectOutputStream(bos);
94          oos.writeObject(m);
95          oos.close();
96          obj.setObjectData(bos.toByteArray());
97      }
98  
99      public synchronized void save()
100             throws TorqueException {
101         if (!modified)
102             return;
103         if (primaryKey == null)
104             return; // FIXME: we need to be able to save the root properties.
105         Connection con = null;
106         try {
107 
108             con = Transaction.begin(StoredBlogPeer.DATABASE_NAME);
109             BaseConfigurable obj = peer.getConfigurable(primaryKey, con);
110             saveTo(obj);
111             obj.save(con);
112             Transaction.commit(con);
113             modified = false;
114         } catch (Exception e) {
115             if (con != null)
116                 Transaction.rollback(con);
117             if (e instanceof TorqueException)
118                 throw (TorqueException)e;
119             throw new TorqueException("Could not save configuration", e);
120         }
121     }
122 
123     public MappedConfiguration(BaseConfigurable obj) {
124         if (obj == null)
125             return;
126         primaryKey = obj.getPrimaryKey();
127         peer = obj.getConfigurablePeer();
128         try {
129             initFromBlog(obj);
130         } catch (Exception e) {
131             log.error("Could not load prefs for " + primaryKey, e);
132         }
133     }
134 
135     protected void addPropertyDirect(String key, Object obj) {
136         super.addPropertyDirect(key, obj);
137         modified = true;
138     }
139 
140     protected List processString(String token) {
141         ArrayList l = new ArrayList(1);
142         l.add(token);
143         return l;
144     }
145 
146     protected String interpolate(String base) {
147         return base;
148     }
149 
150     public void setList(String key, List cont) {
151         setProperty(key, cont);
152     }
153 
154     public String toString() {
155         StringBuffer sb = new StringBuffer("MappedConfiguration (");
156         sb.append(primaryKey);
157         sb.append("/");
158         sb.append(peer.getClass().getName());
159         sb.append(") [\n");
160         ArrayList l = new ArrayList();
161         CollectionUtils.addAll(l, getKeys());
162         Collections.sort(l);
163         for(Iterator i = getKeys(); i.hasNext();) {
164             String key = (String)i.next();
165             sb.append(key);
166             sb.append(" = ");
167             sb.append(getPropertyDirect(key));
168             sb.append("\n");
169         }
170         sb.append(']');
171         return sb.toString();
172     }
173 
174     public boolean isModified() {
175         return modified;
176     }
177 }