KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > repositorylocation > RepositoryLocationImpl


1 /*
2  * Copyright 2004 Hippo Webworks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package nl.hippo.cms.repositorylocation;
17
18 import java.io.InputStream JavaDoc;
19 import java.util.Properties JavaDoc;
20 import nl.hippo.cms.sitesdirectory.SitesDirectoryImpl;
21 import org.apache.avalon.framework.activity.Initializable;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.parameters.ParameterException;
24 import org.apache.avalon.framework.parameters.Parameterizable;
25 import org.apache.avalon.framework.parameters.Parameters;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.excalibur.source.Source;
30 import org.apache.excalibur.source.SourceResolver;
31
32 /**
33  * <p>
34  * A component which knows about the repository in which the content being
35  * edited is stored.
36  * </p>
37  *
38  * <p>
39  * This component is configured using a properties file. The URI can be set
40  * using the parameter named <code>configurationUri</code> .The following
41  * properties must be present. All properties are prefixed with
42  * <code>repository.</code>:
43  * </p>
44  * <ul>
45  * <li>host;</li>
46  * <li>port;</li>
47  * <li>realm;</li>
48  * <li>username;</li>
49  * <li>password;</li>
50  * <li>rootUri;</li>
51  * <li>baseUri.</li>
52  * </ul>
53  *
54  * @author Johan Stuyts
55  */

56 public class RepositoryLocationImpl extends AbstractLogEnabled
57         implements RepositoryLocation, Serviceable, Parameterizable, Initializable
58 {
59     private static final String JavaDoc CONFIGURATION_URI_PARAMETER_NAME = "configurationUri";
60
61     private static final String JavaDoc PROPERTY_PREFIX = "repository.";
62
63     private static final String JavaDoc HOST_PROPERTY_SUFFIX = "host";
64
65     private static final String JavaDoc PORT_PROPERTY_SUFFIX = "port";
66
67     private static final String JavaDoc REALM_PROPERTY_SUFFIX = "realm";
68
69     private static final String JavaDoc USERNAME_PROPERTY_SUFFIX = "username";
70
71     private static final String JavaDoc PASSWORD_PROPERTY_SUFFIX = "password";
72
73     private static final String JavaDoc ROOT_URI_PROPERTY_SUFFIX = "rootUri";
74
75     private static final String JavaDoc BASE_URI_PROPERTY_SUFFIX = "baseUri";
76
77     /**
78      * <p>
79      * The service manager.
80      * </p>
81      */

82     private ServiceManager m_serviceManager;
83
84     /**
85      * <p>
86      * The SourceResolver URI to the properties file with the sites
87      * configuration.
88      * </p>
89      */

90     private String JavaDoc m_configurationUri;
91
92     /**
93      * <p>
94      * The repository information about the repository containing the documents
95      * being edited.
96      * </p>
97      */

98     private RepositoryInformation m_repositoryInformation;
99
100     /**
101      * <p>
102      * Create an instance of this class.
103      * </p>
104      */

105     public RepositoryLocationImpl()
106     {
107         super();
108     }
109
110     /**
111      * <p>
112      * Servicing handler. Called by the service manager so this object can
113      * obtain a reference to it.
114      * </p>
115      *
116      * @param serviceManager
117      * The service manager.
118      */

119     public void service(ServiceManager serviceManager) throws ServiceException
120     {
121         m_serviceManager = serviceManager;
122     }
123
124     /**
125      * <p>
126      * Read the configuration of this component.
127      * </p>
128      *
129      * @param params
130      * The parameters set for this component.
131      */

132     public void parameterize(Parameters params) throws ParameterException
133     {
134         m_configurationUri = params.getParameter(CONFIGURATION_URI_PARAMETER_NAME);
135     }
136
137     /**
138      * <p>
139      * Initialize this component.
140      * </p>
141      */

142     public void initialize() throws Exception JavaDoc
143     {
144         Properties JavaDoc properties = new Properties JavaDoc();
145         Object JavaDoc sourceResolverAsObject = m_serviceManager.lookup(SourceResolver.ROLE);
146         try
147         {
148             if (sourceResolverAsObject instanceof SourceResolver)
149             {
150                 SourceResolver sourceResolver = (SourceResolver) sourceResolverAsObject;
151                 Source source = sourceResolver.resolveURI(m_configurationUri);
152                 try
153                 {
154                     if (source.exists())
155                     {
156                         InputStream JavaDoc input = source.getInputStream();
157                         try
158                         {
159                             properties.load(input);
160                         }
161                         finally
162                         {
163                             input.close();
164                         }
165                     }
166                     else
167                     {
168                         throw new IllegalArgumentException JavaDoc("The properties file specified by '"
169                                 + m_configurationUri + "' must exist.");
170                     }
171                 }
172                 finally
173                 {
174                     sourceResolver.release(source);
175                 }
176             }
177         }
178         finally
179         {
180             m_serviceManager.release(sourceResolverAsObject);
181         }
182         m_repositoryInformation = retrieveRepositoryInformation(properties);
183     }
184
185     /**
186      * Get the repository information about the editor repository.
187      *
188      * @return Repository information about the editor repository.
189      */

190     public RepositoryInformation getRepositoryInformation()
191     {
192         return m_repositoryInformation;
193     }
194
195     /**
196      * <p>
197      * Retrieve the configuration information for a repository.
198      * </p>
199      *
200      * @param properties
201      * The properties containing the configuration.
202      * @param siteInfo
203      * The <code>SiteInformation</code> of the site to which the
204      * repository belongs.
205      * @param repositoryRole
206      * The role of the repository for the site.
207      * @return A <code>RepositoryInformation</code> object with the repository
208      * configuration belonging to the site of <code>siteInfo</code>
209      * and with role <code>repositoryRole</code>.
210      */

211     private RepositoryInformation retrieveRepositoryInformation(Properties JavaDoc properties)
212     {
213         RepositoryInformation result;
214
215         String JavaDoc host = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX
216                 + HOST_PROPERTY_SUFFIX);
217         int port = SitesDirectoryImpl.getRequiredIntegerProperty(properties, PROPERTY_PREFIX
218                 + PORT_PROPERTY_SUFFIX, 1, 65535);
219         String JavaDoc realm = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX
220                 + REALM_PROPERTY_SUFFIX);
221         String JavaDoc username = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX
222                 + USERNAME_PROPERTY_SUFFIX);
223         String JavaDoc password = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX
224                 + PASSWORD_PROPERTY_SUFFIX);
225         String JavaDoc rootUri = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX
226                 + ROOT_URI_PROPERTY_SUFFIX);
227         String JavaDoc baseUri = SitesDirectoryImpl.getRequiredProperty(properties, PROPERTY_PREFIX
228                 + BASE_URI_PROPERTY_SUFFIX);
229         result = new RepositoryInformation(host, port, realm, username, password, rootUri, baseUri);
230
231         return result;
232     }
233 }
234
Popular Tags