Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/obinary/cms/beans/ServerInfo.java


1   /**
2    *
3    * Magnolia and its source-code is licensed under the LGPL.
4    * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5    * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6    * you are required to provide proper attribution to obinary.
7    * If you reproduce or distribute the document without making any substantive modifications to its content,
8    * please use the following attribution line:
9    *
10   * Copyright 1993-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11   *
12   * */
13  
14  
15  
16  
17  package com.obinary.cms.beans;
18  
19  
20  import com.obinary.cms.core.*;
21  import com.obinary.cms.util.URIStringComparator;
22  
23  import javax.jcr.ElementNotFoundException;
24  import javax.jcr.RepositoryException;
25  import javax.jcr.Ticket;
26  import javax.jcr.PasswordCredentials;
27  import java.util.Iterator;
28  import java.util.Hashtable;
29  import java.util.Enumeration;
30  import java.util.regex.Pattern;
31  
32  import jdsl.core.ref.ArraySequence;
33  import jdsl.core.algo.sorts.HeapSort;
34  import jdsl.core.api.ObjectIterator;
35  
36  
37  
38  /**
39   * User: sameercharles
40   * Date: Jun 2, 2003
41   * Time: 3:06:43 PM
42   * @author Sameer Charles
43   * @version 1.0
44   */
45  
46  
47  public class ServerInfo {
48  
49  
50  
51      private static final String START_PAGE = "/server";
52  
53      private static Hashtable cachedContent;
54      private static Hashtable cachedURImapping = new Hashtable();
55      private static Hashtable cachedCacheableURIMapping = new Hashtable();
56      private static Ticket masterTicket;
57  
58  
59  
60      /**
61       * constructor
62       */
63      public ServerInfo() {
64  
65      }
66  
67  
68  
69      /**
70       * constructor Initializes and cache content
71       *
72       * @param init
73       * @throws javax.jcr.ElementNotFoundException
74       * @throws javax.jcr.RepositoryException
75       */
76      public ServerInfo(boolean init) throws ElementNotFoundException, RepositoryException {
77          ServerInfo.init();
78      }
79  
80  
81  
82      /**
83       *
84       * @throws javax.jcr.ElementNotFoundException
85       * @throws javax.jcr.RepositoryException
86       */
87      public static void init() throws ElementNotFoundException, RepositoryException {
88          HierarchyManager hm = new HierarchyManager();
89          hm.setStartPage(ConfigLoader.configRoot);
90          Content startPage = hm.getPage(START_PAGE);
91          ServerInfo.cacheContent(startPage);
92          /* create a master ticket which will be used to access repository content */
93          ServerInfo.masterTicket = ConfigLoader.websiteRepository.connect(new PasswordCredentials("superuser", "".toCharArray()));
94      }
95  
96  
97  
98      /**
99       * <p>Cache server content from the config repository</p>
100      *
101      */
102     private static void cacheContent(Content page) {
103         ServerInfo.cachedContent = new Hashtable();
104         try {
105             String isProtected = page.getAtom("protected").getValue().getString();
106             ServerInfo.cachedContent.put("protected",isProtected);
107         } catch (RepositoryException re) {
108             ServerInfo.cachedContent.put("protected","false");
109         }
110         try {
111             String isAdmin = page.getAtom("admin").getValue().getString();
112             ServerInfo.cachedContent.put("admin",isAdmin);
113         } catch (RepositoryException re) {
114             ServerInfo.cachedContent.put("admin","false");
115         }
116         try {
117             String ext = page.getAtom("defaultExtension").getValue().getString();
118             ServerInfo.cachedContent.put("defaultExtension",ext);
119         } catch (RepositoryException re) {
120             ServerInfo.cachedContent.put("defaultExtension","");
121         }
122         try {
123             String mailServer = page.getAtom("defaultMailServer").getString();
124             ServerInfo.cachedContent.put("defaultMailServer",mailServer);
125         } catch (Exception e) {
126             ServerInfo.cachedContent.put("defaultMailServer","");
127         }
128         try {
129             ServerInfo.cachedContent.put("404URI",page.getAtom("ResourceNotAvailableURIMapping").getString());
130             ContainerList cl = page.getContainerList("URIMapping");
131             cacheURIMappings(cl);
132         } catch (RepositoryException re) {}
133         try {
134             ServerInfo.cachedContent.put("cacheable",page.getAtom("cacheable").getString());
135             ContainerList cl = page.getContainerList("cacheMapping");
136             cacheCacheableURIMappings(cl);
137         } catch (RepositoryException re) {}
138     }
139 
140 
141 
142     /**
143      * @param containerList to be added in cache
144      */
145     private static void cacheURIMappings(ContainerList containerList) {
146         Iterator it = containerList.getChildren().iterator();
147         ArraySequence as = new ArraySequence();
148         while (it.hasNext()) {
149             as.insertLast(it.next());
150         }
151         HeapSort hs = new HeapSort();
152         hs.sort(as,new URIStringComparator());
153         ObjectIterator oi = as.elements();
154         while (oi.hasNext()) {
155             Container container = (Container)oi.nextObject();
156             Atom fromURI = container.getAtom("fromURI");
157             StringBuffer fromURIStringBuffer = new StringBuffer();
158             char[] chars = fromURI.getString().toCharArray();
159             int i = 0, last = 0;
160             while (i < chars.length) {
161                 char c = chars[i];
162                 if (c == '*') {
163                     fromURIStringBuffer.append(chars, last, i - last);
164                     fromURIStringBuffer.append("[a-z[A-Z[./[0-9]]]]*");
165                     last = i+1;
166                 }
167                 i++;
168             }
169             fromURIStringBuffer.append(chars, last, i - last);
170             Pattern p = Pattern.compile(fromURIStringBuffer.toString());
171             ServerInfo.cachedURImapping.put(p,container.getAtom("toURI").getString());
172         }
173     }
174 
175 
176 
177     /**
178      *
179      * @param containerList to be added in cache
180      */
181     private static void cacheCacheableURIMappings(ContainerList containerList) {
182         if (containerList == null)
183             return;
184         Iterator it = containerList.getChildren().iterator();
185         ArraySequence as = new ArraySequence();
186         while (it.hasNext()) {
187             as.insertLast(it.next());
188         }
189         HeapSort hs = new HeapSort();
190         hs.sort(as,new URIStringComparator());
191         ObjectIterator oi = as.elements();
192         while (oi.hasNext()) {
193             Container container = (Container)oi.nextObject();
194             Atom URI = container.getAtom("URI");
195             StringBuffer URIStringBuffer = new StringBuffer();
196             char[] chars = URI.getString().toCharArray();
197             int i = 0, last = 0;
198             while (i < chars.length) {
199                 char c = chars[i];
200                 if (c == '*') {
201                     URIStringBuffer.append(chars, last, i - last);
202                     URIStringBuffer.append("[a-z[A-Z[./[0-9]]]]*");
203                     last = i+1;
204                 }
205                 i++;
206             }
207             URIStringBuffer.append(chars, last, i - last);
208             Pattern p = Pattern.compile(URIStringBuffer.toString());
209             ServerInfo.cachedCacheableURIMapping.put(p,"");
210         }
211         CacheHandler.validatePath(CacheHandler.CACHE_DIRECTORY);
212     }
213 
214 
215 
216     /**
217      *
218      * @return resource not available URI mapping as specifies in serverInfo, else /
219      */
220     public static String get404URI() {
221         String URI = (String)ServerInfo.cachedContent.get("404URI");
222         if (URI.equals(""))
223             return "/";
224         return URI;
225     }
226 
227 
228 
229     /**
230      * <p>
231      * checks for the requested URI mapping in ServerInfo
232      * : Servlet Specification 2.3 Section 10 "Mapping Requests to Servlets"
233      * </p>
234      *
235      * @return URI string mapping
236      */
237     public static String getURIMapping(String uri) {
238         Enumeration e = ServerInfo.cachedURImapping.keys();
239         while (e.hasMoreElements()) {
240             Pattern p = (Pattern)e.nextElement();
241             if (p.matcher(uri).matches())
242                 return (String)ServerInfo.cachedURImapping.get(p);
243         }
244         return "";
245     }
246 
247 
248 
249     /**
250      * <p>checks if the current context is protected ot not
251      * this will be changed in version 2 , will be moved to separate coinfig
252      * which will allow to define multiple context</p>
253      *
254      * @return boolean
255      */
256     public static boolean isContextProtected() {
257         return (((String)ServerInfo.cachedContent.get("protected")).equals("true"));
258     }
259 
260 
261 
262     /**
263      * @return default URL extension as configured
264      */
265     public static String getDefaultExtension() {
266         return (String)ServerInfo.cachedContent.get("defaultExtension");
267     }
268 
269 
270     /**
271      * @return default mail server
272      * */
273     public static String getDefaultMailServer() {
274         return (String)ServerInfo.cachedContent.get("defaultMailServer");
275     }
276 
277 
278     /**
279      * @return true if the instance is Admin
280      */
281     public static boolean isAdmin() {
282         return (((String)ServerInfo.cachedContent.get("admin")).equals("true"));
283     }
284 
285 
286 
287     /**
288      * @return true if the pages could be cached
289      */
290     public static boolean isCacheable() {
291         return (((String)ServerInfo.cachedContent.get("cacheable")).equals("true"));
292     }
293 
294 
295 
296     /**
297      * @return true if the requested URI can be added to cache
298      */
299     public static boolean isCacheable(String uri) {
300         Enumeration e = ServerInfo.cachedCacheableURIMapping.keys();
301         while (e.hasMoreElements()) {
302             Pattern p = (Pattern)e.nextElement();
303             if (p.matcher(uri).matches())
304                 return true;
305         }
306         return false;
307     }
308 
309 
310     /**
311      *
312      */
313     public static Ticket getMasterTicket() {
314         return ServerInfo.masterTicket;
315     }
316 
317 
318 }