KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > ConfigManagerImpl


1 /*
2  * Created on Feb 4, 2004
3  */

4 package org.roller.business;
5
6 import org.apache.commons.lang.StringUtils;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.roller.RollerException;
10 import org.roller.model.ConfigManager;
11 import org.roller.model.Roller;
12 import org.roller.pojos.RollerConfigData;
13 import org.roller.util.OldRollerConfig;
14
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.math.BigDecimal JavaDoc;
18
19 /**
20  * Abstract base implementation using PersistenceStrategy.
21  * @author Dave Johnson
22  * @author Lance Lavandowska
23  */

24 public abstract class ConfigManagerImpl implements ConfigManager {
25
26     private static Log mLogger =
27         LogFactory.getFactory().getInstance(ConfigManagerImpl.class);
28
29     protected PersistenceStrategy mStrategy;
30
31     public ConfigManagerImpl(PersistenceStrategy strategy)
32     {
33         mStrategy = strategy;
34     }
35
36     public void release()
37     {
38     }
39
40     /**
41      * @see org.roller.model.ConfigManager#storeRollerConfig(org.roller.pojos.RollerConfig)
42      */

43     public void storeRollerConfig(RollerConfigData data) throws RollerException
44     {
45         mStrategy.store(data);
46     }
47
48     /**
49      * Take the RollerConfigFile read from roller-config.xml
50      * and set the values into a new RollerConfig instance.
51      * Some values will need be converted from List to comma-delimited String.
52      */

53     private RollerConfigData newConfigFromFile(OldRollerConfig file) throws RollerException
54     {
55         RollerConfigData config = new RollerConfigData();
56         config.setAbsoluteURL ( file.getAbsoluteURL() );
57         config.setRssUseCache ( Boolean.valueOf(file.getRssUseCache()) );
58         config.setRssCacheTime ( new Integer JavaDoc(file.getRssCacheTime()) );
59         config.setNewUserAllowed ( Boolean.valueOf(file.getNewUserAllowed()) );
60         config.setAdminUsers ( StringUtils.join( file.getAdminUsers().toArray(), ",") );
61         config.setUserThemes ( file.getNewUserThemes() );
62         config.setEditorPages ( StringUtils.join( file.getEditorPages().toArray(), ",") );
63         config.setEnableAggregator ( Boolean.valueOf(file.getEnableAggregator()) );
64         config.setUploadEnabled ( Boolean.valueOf(file.getUploadEnabled()) );
65         config.setUploadMaxDirMB ( new BigDecimal JavaDoc(file.getUploadMaxDirMB().doubleValue()).setScale(2) );
66         config.setUploadMaxFileMB ( new BigDecimal JavaDoc(file.getUploadMaxFileMB().doubleValue()).setScale(2) );
67         config.setUploadAllow ( StringUtils.join( file.getUploadAllow().toArray(), ",") );
68         config.setUploadForbid ( StringUtils.join( file.getUploadForbid().toArray(), ",") );
69         config.setUploadDir ( file.getUploadDir() );
70         config.setUploadPath ( file.getUploadPath() );
71         config.setMemDebug ( Boolean.valueOf(file.getMemDebug()) );
72         config.setAutoformatComments ( Boolean.valueOf(file.getAutoformatComments()) );
73         config.setEscapeCommentHtml ( Boolean.valueOf(file.getEscapeCommentHtml()) );
74         config.setEmailComments ( Boolean.valueOf(file.getEmailComments()) );
75         config.setEnableLinkback ( Boolean.valueOf(file.isEnableLinkback()) );
76         config.setSiteName ( file.getSiteName() );
77         config.setSiteDescription ( file.getSiteDescription() );
78         config.setEmailAddress ( file.getEmailAddress() );
79         config.setIndexDir ( file.getIndexDir() );
80         config.setEncryptPasswords( Boolean.valueOf(file.getEncryptPasswords()) );
81         return config;
82     }
83
84     /**
85      * Read in RollerConfig from the indicated filePath or, if the file cannot be found
86      * returns a freshly initialized RollerConfig object.
87      * @param filePath where roller-config.xml can be found.
88      */

89     public RollerConfigData readFromFile(String JavaDoc filePath) throws RollerException
90     {
91         // Try to read roller-config.xml
92
OldRollerConfig file = null;
93         java.io.InputStream JavaDoc in = null;
94         try
95         {
96             in = new FileInputStream JavaDoc(filePath);
97             file = OldRollerConfig.readConfig(in);
98         }
99         catch (FileNotFoundException JavaDoc fnf)
100         {
101             mLogger.info("roller-config.xml not found, creating new: "+ filePath);
102                 return new RollerConfigData();
103         }
104         catch (SecurityException JavaDoc se)
105         {
106             throw new RuntimeException JavaDoc(se);
107         }
108         finally
109         {
110             try
111             {
112                 if (in != null)
113                     in.close();
114             }
115             catch (java.io.IOException JavaDoc ioe)
116             {
117                 mLogger.warn("ERROR closing InputStream", ioe);
118             }
119         }
120
121         if (file == null)
122         {
123             file = new OldRollerConfig();
124             mLogger.warn("Unable to read in roller-config.xml, creating 'blank' RollerConfigFile");
125         }
126         return newConfigFromFile(file);
127     }
128
129     /**
130      * This isn't part of the ConfigManager Interface, because really
131      * we shouldn't ever delete the RollerConfig. This is mostly here
132      * to assist with unit testing.
133      */

134     public void removeRollerConfig(String JavaDoc id) throws RollerException
135     {
136         mStrategy.remove(id,RollerConfigData.class);
137     }
138 }
139
Popular Tags