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

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.impl.ClassLoaderUtil;
32  import org.apache.chemistry.opencmis.commons.server.CmisServiceFactory;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * CMIS context listener.
38   */
39  public class CmisRepositoryContextListener implements ServletContextListener {
40  
41      public static final String SERVICES_FACTORY = "org.apache.chemistry.opencmis.servicesfactory";
42  
43      private static final Logger LOG = LoggerFactory.getLogger(CmisRepositoryContextListener.class.getName());
44  
45      private static final String CONFIG_INIT_PARAM = "org.apache.chemistry.opencmis.REPOSITORY_CONFIG_FILE";
46      private static final String CONFIG_FILENAME = "/repository.properties";
47      private static final String PROPERTY_CLASS = "class";
48  
49      public void contextInitialized(ServletContextEvent sce) {
50          // get config file name or use default
51          String configFilename = sce.getServletContext().getInitParameter(CONFIG_INIT_PARAM);
52          if (configFilename == null) {
53              configFilename = CONFIG_FILENAME;
54          }
55  
56          // create services factory
57          CmisServiceFactory factory = createServiceFactory(configFilename);
58  
59          // set the services factory into the servlet context
60          sce.getServletContext().setAttribute(SERVICES_FACTORY, factory);
61      }
62  
63      public void contextDestroyed(ServletContextEvent sce) {
64          // destroy services factory
65          CmisServiceFactory factory = (CmisServiceFactory) sce.getServletContext().getAttribute(SERVICES_FACTORY);
66          if (factory != null) {
67              factory.destroy();
68          }
69      }
70  
71      /**
72       * Creates a service factory.
73       */
74      private CmisServiceFactory createServiceFactory(String filename) {
75          // load properties
76          InputStream stream = this.getClass().getResourceAsStream(filename);
77  
78          if (stream == null) {
79              LOG.warn("Cannot find configuration!");
80              return null;
81          }
82  
83          Properties props = new Properties();
84          try {
85              props.load(stream);
86          } catch (IOException e) {
87              LOG.warn("Cannot load configuration: " + e, e);
88              return null;
89          } finally {
90              try {
91                  stream.close();
92              } catch (IOException ioe) {
93              }
94          }
95  
96          // get 'class' property
97          String className = props.getProperty(PROPERTY_CLASS);
98          if (className == null) {
99              LOG.warn("Configuration doesn't contain the property 'class'!");
100             return null;
101         }
102 
103         // create a factory instance
104         Object object = null;
105         try {
106             object = ClassLoaderUtil.loadClass(className).newInstance();
107         } catch (Exception e) {
108             LOG.warn("Could not create a services factory instance: " + e, e);
109             return null;
110         }
111 
112         if (!(object instanceof CmisServiceFactory)) {
113             LOG.warn("The provided class is not an instance of CmisServiceFactory!");
114         }
115 
116         CmisServiceFactory factory = (CmisServiceFactory) object;
117 
118         // initialize factory instance
119         Map<String, String> parameters = new HashMap<String, String>();
120 
121         for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements();) {
122             String key = (String) e.nextElement();
123             String value = props.getProperty(key);
124             parameters.put(key, value);
125         }
126 
127         factory.init(parameters);
128 
129         LOG.info("Initialized Services Factory: " + factory.getClass().getName());
130 
131         return factory;
132     }
133 }