This project has retired. For details please refer to its Attic page.
CmisRepositoryContextListener xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import javax.servlet.ServletContextEvent;
29  import javax.servlet.ServletContextListener;
30  
31  import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  /**
36   * CMIS context listener.
37   */
38  public class CmisRepositoryContextListener implements ServletContextListener {
39  
40      public static final String SERVICES_FACTORY = "org.apache.chemistry.opencmis.servicesfactory";
41  
42      private static final Log log = LogFactory.getLog(CmisRepositoryContextListener.class.getName());
43  
44      private static final String CONFIG_INIT_PARAM = "org.apache.chemistry.opencmis.REPOSITORY_CONFIG_FILE";
45      private static final String CONFIG_FILENAME = "/repository.properties";
46      private static final String PROPERTY_CLASS = "class";
47  
48      public void contextInitialized(ServletContextEvent sce) {
49          // get config file name or use default
50          String configFilename = sce.getServletContext().getInitParameter(CONFIG_INIT_PARAM);
51          if (configFilename == null) {
52              configFilename = CONFIG_FILENAME;
53          }
54  
55          // create services factory
56          CmisServiceFactory factory = createServiceFactory(configFilename);
57  
58          // set the services factory into the servlet context
59          sce.getServletContext().setAttribute(SERVICES_FACTORY, factory);
60      }
61  
62      public void contextDestroyed(ServletContextEvent sce) {
63          // destroy services factory
64          CmisServiceFactory factory = (CmisServiceFactory) sce.getServletContext().getAttribute(SERVICES_FACTORY);
65          if (factory != null) {
66              factory.destroy();
67          }
68      }
69  
70      /**
71       * Creates a service factory.
72       */
73      private CmisServiceFactory createServiceFactory(String filename) {
74          // load properties
75          InputStream stream = this.getClass().getResourceAsStream(filename);
76  
77          if (stream == null) {
78              log.warn("Cannot find configuration!");
79              return null;
80          }
81  
82          Properties props = new Properties();
83          try {
84              props.load(stream);
85          } catch (IOException e) {
86              log.warn("Cannot load configuration: " + e, e);
87              return null;
88          } finally {
89              try {
90                  stream.close();
91              } catch (IOException ioe) {
92              }
93          }
94  
95          // get 'class' property
96          String className = props.getProperty(PROPERTY_CLASS);
97          if (className == null) {
98              log.warn("Configuration doesn't contain the property 'class'!");
99              return null;
100         }
101 
102         // create a factory instance
103         Object object = null;
104         try {
105             object = Class.forName(className).newInstance();
106         } catch (Exception e) {
107             log.warn("Could not create a services factory instance: " + e, e);
108             return null;
109         }
110 
111         if (!(object instanceof CmisServiceFactory)) {
112             log.warn("The provided class is not an instance of CmisServiceFactory!");
113         }
114 
115         CmisServiceFactory factory = (CmisServiceFactory) object;
116 
117         // initialize factory instance
118         Map<String, String> parameters = new HashMap<String, String>();
119 
120         for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
121             String key = (String) e.nextElement();
122             String value = props.getProperty(key);
123             parameters.put(key, value);
124         }
125 
126         factory.init(parameters);
127 
128         log.info("Initialized Services Factory: " + factory.getClass().getName());
129 
130         return factory;
131     }
132 }