View Javadoc

1   /*
2    * Copyright 2008 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.client.webapp;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.Properties;
21  
22  import javax.servlet.ServletConfig;
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServlet;
25  
26  import nl.hippo.client.api.ClientException;
27  import nl.hippo.client.api.service.CachingService;
28  import nl.hippo.client.api.service.NOPCachingService;
29  import nl.hippo.client.api.service.UpdateNotificationService;
30  import nl.hippo.client.api.service.WebdavService;
31  import nl.hippo.client.caching.JCSDefaultCache;
32  import nl.hippo.client.caching.service.CachingServiceImpl;
33  import nl.hippo.client.event.service.UpdateNotificationConfig;
34  import nl.hippo.client.event.service.UpdateNotificationServiceImpl;
35  import nl.hippo.client.webdav.WebdavConfig;
36  import nl.hippo.client.webdav.caching.WebdavCacheHandler;
37  import nl.hippo.client.webdav.service.WebdavServiceImpl;
38  
39  public class RepositoryAdapter extends HttpServlet {
40      private static final long serialVersionUID = 1L;
41  
42      public static final String DOCUMENTS_WEBDAV = "documents";
43      public static final String BINARIES_WEBDAV = "binaries";
44      public static final String QUERIES_WEBDAV = "queries";
45      
46      public static final String QUERY_PATH = "configuration/adapter";
47  
48      public void init(ServletConfig servletConfig) throws ServletException {
49          super.init(servletConfig);
50          WebAppLogger.log.debug("Initializing Repository Adapter Servlet");
51  
52          try {           
53              ServiceWrapper documentService = setupService(DOCUMENTS_WEBDAV);
54              getServletContext().setAttribute(DOCUMENTS_WEBDAV, documentService);
55              
56              ServiceWrapper binariesService = setupService(BINARIES_WEBDAV);
57              getServletContext().setAttribute(BINARIES_WEBDAV, binariesService);
58  
59              ServiceWrapper queriesService = setupService(QUERIES_WEBDAV);
60              getServletContext().setAttribute(QUERIES_WEBDAV, queriesService);
61  
62              //This is the directory in the repository where queries and transformers are stored
63              //Attempt to create it in case it doesn't exist yet.
64              String path = QUERY_PATH.substring(0, QUERY_PATH.lastIndexOf("/"));
65              queriesService.executeMkCol(queriesService.getBasePath().createRelativePath(path));
66              queriesService.executeMkCol(queriesService.getBasePath().createRelativePath(QUERY_PATH));
67  
68              WebAppLogger.log.info("Repository Adapter Servlet initialized");
69          } catch (ClientException e) {
70              WebAppLogger.log.error("failed to set up repository adapter", e);
71          }
72      }
73  
74      public void destroy() {
75          getServletContext().removeAttribute(DOCUMENTS_WEBDAV);
76          getServletContext().removeAttribute(BINARIES_WEBDAV);
77          getServletContext().removeAttribute(QUERIES_WEBDAV);
78      }
79  
80      private ServiceWrapper setupService(String key) throws ServletException, ClientException {
81          Properties properties = readConfigFile(key + ".properties");
82          WebdavConfig webdavConfig = new WebdavConfig(properties);
83  
84          CachingService cache;
85          if ("true".equals(properties.getProperty("hippo.client.caching"))) {
86  
87              UpdateNotificationConfig updatenotifierConfig = new UpdateNotificationConfig(properties);
88              UpdateNotificationService updateNotifier = new UpdateNotificationServiceImpl(updatenotifierConfig);
89              updateNotifier.start();
90              WebAppLogger.log.debug("UpdateNotifier initialized");
91  
92              String namespace = webdavConfig.getNamespace();
93              cache = new CachingServiceImpl(new JCSDefaultCache("default"), updateNotifier.getEventAwareManager(),
94                      namespace);
95              WebAppLogger.log.debug("Cachemanager initialized");
96          } else {
97              cache = new NOPCachingService();
98          }
99          WebdavService webdavService = new WebdavServiceImpl(webdavConfig, cache);
100         WebdavCacheHandler cacheHandler = new WebdavCacheHandler(cache);
101         
102         return new ServiceWrapper(webdavService, cacheHandler);
103     }
104 
105     private Properties readConfigFile(String file) throws ServletException {
106         Properties props = new Properties();
107         InputStream propStream = getServletContext().getResourceAsStream(file);
108         //InputStream propStream = getClass().getClassLoader().getResourceAsStream(file);
109         if (propStream == null) {
110             throw new ServletException("Unable to find " + file);
111         }
112         try {
113             props.load(propStream);
114         } catch (IOException e) {
115             throw new ServletException("Cannot load " + file + ": " + e.getMessage(), e);
116         }
117         return props;
118     }
119 
120 }