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.server.CmisServiceFactory;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
36
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
50 String configFilename = sce.getServletContext().getInitParameter(CONFIG_INIT_PARAM);
51 if (configFilename == null) {
52 configFilename = CONFIG_FILENAME;
53 }
54
55
56 CmisServiceFactory factory = createServiceFactory(configFilename);
57
58
59 sce.getServletContext().setAttribute(SERVICES_FACTORY, factory);
60 }
61
62 public void contextDestroyed(ServletContextEvent sce) {
63
64 CmisServiceFactory factory = (CmisServiceFactory) sce.getServletContext().getAttribute(SERVICES_FACTORY);
65 if (factory != null) {
66 factory.destroy();
67 }
68 }
69
70
71
72
73 private CmisServiceFactory createServiceFactory(String filename) {
74
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
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
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
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 }