1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.fosstrak.epcis.repository.query;
22
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Properties;
27
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import javax.servlet.ServletConfig;
32 import javax.servlet.ServletContext;
33 import javax.servlet.ServletException;
34 import javax.sql.DataSource;
35 import javax.xml.ws.Endpoint;
36
37 import org.fosstrak.epcis.soap.EPCISServicePortType;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.cxf.BusFactory;
41 import org.apache.cxf.interceptor.LoggingInInterceptor;
42 import org.apache.cxf.interceptor.LoggingOutInterceptor;
43 import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class QueryInitServlet extends CXFNonSpringServlet {
58
59 private static final long serialVersionUID = -5839101192038037389L;
60
61 private static final String APP_CONFIG_LOCATION = "appConfigLocation";
62 private static final String PROP_MAX_QUERY_ROWS = "maxQueryResultRows";
63 private static final String PROP_MAX_QUERY_TIME = "maxQueryExecutionTime";
64 private static final String PROP_TRIGGER_CHECK_SEC = "trigger.condition.check.sec";
65 private static final String PROP_TRIGGER_CHECK_MIN = "trigger.condition.check.min";
66 private static final String PROP_SERVICE_VERSION = "service.version";
67 private static final String PROP_JNDI_DATASOURCE_NAME = "jndi.datasource.name";
68
69 private static final Log LOG = LogFactory.getLog(QueryInitServlet.class);
70
71 private Properties properties;
72
73
74
75
76
77
78 public void loadBus(ServletConfig servletConfig) throws ServletException {
79 super.loadBus(servletConfig);
80 BusFactory.setDefaultBus(getBus());
81 if (LOG.isDebugEnabled()) {
82 getBus().getInInterceptors().add(new LoggingInInterceptor());
83 getBus().getOutInterceptors().add(new LoggingOutInterceptor());
84 getBus().getOutFaultInterceptors().add(new LoggingOutInterceptor());
85 getBus().getInFaultInterceptors().add(new LoggingInInterceptor());
86 }
87 EPCISServicePortType service = setupQueryOperationsModule(servletConfig);
88
89 LOG.debug("Publishing query operations module service at /query");
90 Endpoint.publish("/query", service);
91 }
92
93 private EPCISServicePortType setupQueryOperationsModule(ServletConfig servletConfig) {
94 loadApplicationProperties(servletConfig);
95 String jndiName = properties.getProperty(PROP_JNDI_DATASOURCE_NAME);
96 DataSource dataSource = loadDataSource(jndiName);
97
98 LOG.debug("Initializing query operations module");
99 QueryOperationsModule module = new QueryOperationsModule();
100 module.setMaxQueryRows(Integer.parseInt(properties.getProperty(PROP_MAX_QUERY_ROWS)));
101 module.setMaxQueryTime(Integer.parseInt(properties.getProperty(PROP_MAX_QUERY_TIME)));
102 module.setTriggerConditionMinutes(properties.getProperty(PROP_TRIGGER_CHECK_MIN));
103 module.setTriggerConditionSeconds(properties.getProperty(PROP_TRIGGER_CHECK_SEC));
104 module.setServiceVersion(properties.getProperty(PROP_SERVICE_VERSION));
105 module.setDataSource(dataSource);
106 module.setServletContext(servletConfig.getServletContext());
107 module.setBackend(new QueryOperationsBackendSQL());
108
109 LOG.debug("Initializing query operations web service");
110 QueryOperationsWebService service = new QueryOperationsWebService(module);
111 return service;
112 }
113
114
115
116
117
118
119
120
121
122 private void loadApplicationProperties(ServletConfig servletConfig) {
123 properties = new Properties();
124
125
126 String path = "/";
127 String appConfigFile = "application.properties";
128 InputStream is = QueryInitServlet.class.getResourceAsStream(path + appConfigFile);
129
130 try {
131 if (is == null) {
132
133 ServletContext ctx = servletConfig.getServletContext();
134 path = ctx.getRealPath("/");
135 appConfigFile = ctx.getInitParameter(APP_CONFIG_LOCATION);
136 is = new FileInputStream(path + appConfigFile);
137 }
138 properties.load(is);
139 is.close();
140 LOG.info("Loaded application properties from " + path + appConfigFile);
141 } catch (IOException e) {
142 LOG.error("Unable to load application properties from " + path + appConfigFile, e);
143 }
144 }
145
146
147
148
149
150
151
152
153
154 private DataSource loadDataSource(String jndiName) {
155 DataSource dataSource = null;
156 try {
157 Context ctx = new InitialContext();
158 dataSource = (DataSource) ctx.lookup(jndiName);
159 LOG.info("Loaded data source via JNDI from " + jndiName);
160 } catch (NamingException e) {
161 LOG.error("Unable to load data source via JNDI from " + jndiName, e);
162 }
163 return dataSource;
164 }
165 }