This project has retired. For details please refer to its
Attic page.
CmisRepositoryContextListener xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
51 String configFilename = sce.getServletContext().getInitParameter(CONFIG_INIT_PARAM);
52 if (configFilename == null) {
53 configFilename = CONFIG_FILENAME;
54 }
55
56
57 CmisServiceFactory factory = createServiceFactory(configFilename);
58
59
60 sce.getServletContext().setAttribute(SERVICES_FACTORY, factory);
61 }
62
63 public void contextDestroyed(ServletContextEvent sce) {
64
65 CmisServiceFactory factory = (CmisServiceFactory) sce.getServletContext().getAttribute(SERVICES_FACTORY);
66 if (factory != null) {
67 factory.destroy();
68 }
69 }
70
71
72
73
74 private CmisServiceFactory createServiceFactory(String filename) {
75
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
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
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
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 }