KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > urlrewriting > URLRewritingService


1 package org.jahia.services.urlrewriting;
2
3 import org.jahia.services.JahiaInitializableService;
4 import org.jahia.settings.SettingsBean;
5 import org.jahia.exceptions.JahiaInitializationException;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import org.apache.commons.digester.Digester;
9 import org.apache.commons.digester.Rule;
10 import java.util.Properties JavaDoc;
11 import java.io.IOException JavaDoc;
12 import org.xml.sax.SAXException JavaDoc;
13 import java.io.File JavaDoc;
14 import java.lang.reflect.Constructor JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16
17 /**
18  * <p>Title: </p>
19  * <p>Description: </p>
20  * <p>Copyright: Copyright (c) 2002</p>
21  * <p>Company: Jahia Ltd</p>
22  * @author Serge Huber
23  * @version 1.0
24  */

25
26 public class URLRewritingService extends JahiaInitializableService {
27
28     private static org.apache.log4j.Logger logger =
29         org.apache.log4j.Logger.getLogger(URLRewritingService.class);
30
31     private static URLRewritingService singletonInstance = null;
32
33     private Digester digester;
34     private ArrayList JavaDoc rewriteRules = new ArrayList JavaDoc();
35
36     private URLRewritingService() {
37     }
38
39     /**
40      * Return the unique service instance. If the instance does not exist,
41      * a new instance is created.
42      *
43      * @return The unique service instance.
44      */

45     public synchronized static URLRewritingService getInstance() {
46         if (singletonInstance == null) {
47             singletonInstance = new URLRewritingService();
48         }
49         return singletonInstance;
50     }
51
52
53     /**
54      * Initializes the servlet dispatching service with parameters loaded
55      * from the Jahia configuration file.
56      * @param jSettings private settings object that contains Jahia
57      * configuration parameters
58      * @throws JahiaInitializationException thrown in the case of an error
59      * during this initialization, that will be treated as a critical error
60      * in Jahia and probably stop execution of Jahia once and for all.
61      */

62     public void init (SettingsBean jSettings)
63         throws JahiaInitializationException {
64         try {
65             load(jSettings.getJahiaEtcDiskPath() + File.separator + "config" +
66                  File.separator + "url-rewriting.xml");
67         } catch (Throwable JavaDoc t) {
68             throw new JahiaInitializationException("Error while loading URL rewriting configuration file", t);
69         }
70     }
71
72     public String JavaDoc getOriginalFromRewritten(String JavaDoc rewrittenURL) {
73         Iterator JavaDoc ruleIter = rewriteRules.iterator();
74         while (ruleIter.hasNext()) {
75             RewriteRule curRule = (RewriteRule) ruleIter.next();
76             String JavaDoc originalURL = curRule.getOriginalFromRewritten(rewrittenURL);
77             if (originalURL != null) {
78                 return originalURL;
79             }
80         }
81         return null;
82     }
83
84     public String JavaDoc getRewrittenFromOriginal(String JavaDoc originalURL) {
85         Iterator JavaDoc ruleIter = rewriteRules.iterator();
86         while (ruleIter.hasNext()) {
87             RewriteRule curRule = (RewriteRule) ruleIter.next();
88             String JavaDoc rewrittenURL = curRule.getRewrittenFromOriginal(originalURL);
89             if (rewrittenURL != null) {
90                 return rewrittenURL;
91             }
92         }
93         return null;
94
95     }
96
97     private void load(String JavaDoc urlRewritingConfigFileName)
98         throws IOException JavaDoc, SAXException JavaDoc {
99         initDigester();
100         File JavaDoc urlRewritingConfigFile = new File JavaDoc(urlRewritingConfigFileName);
101         this.digester.parse(urlRewritingConfigFile);
102     }
103
104     private void initDigester() {
105         this.digester = new Digester();
106
107         AddRewriteRuleRule addRewriteRuleRule =
108             new AddRewriteRuleRule();
109         digester.addRule(
110             "rewrite-rules/rule",
111             addRewriteRuleRule);
112         digester.addRule(
113             "rewrite-rules/rule/original-from-rewritten/property",
114             addRewriteRuleRule.addOriginalFromRewrittenPropsRule);
115         digester.addRule(
116             "rewrite-rules/rule/rewritten-from-original/property",
117             addRewriteRuleRule.addRewrittenFromOriginalPropsRule);
118     }
119
120     final class AddRewriteRuleRule extends Rule {
121
122         private Properties JavaDoc originalFromRewrittenProps = new Properties JavaDoc();
123         private Properties JavaDoc rewrittenFromOriginalProps = new Properties JavaDoc();
124
125         AddPropertyRule addOriginalFromRewrittenPropsRule = new AddPropertyRule(originalFromRewrittenProps);
126         AddPropertyRule addRewrittenFromOriginalPropsRule = new AddPropertyRule(rewrittenFromOriginalProps);
127
128         private Properties JavaDoc properties = new Properties JavaDoc();
129
130         public void begin(
131             String JavaDoc namespace,
132             String JavaDoc name,
133             org.xml.sax.Attributes JavaDoc attributes)
134             throws Exception JavaDoc {
135             for (int i = 0; i < attributes.getLength(); i++) {
136                 properties.setProperty(
137                     attributes.getQName(i),
138                     attributes.getValue(i));
139             }
140         }
141
142         public void end(String JavaDoc namespace, String JavaDoc name) throws Exception JavaDoc {
143             String JavaDoc className = properties.getProperty("class");
144             try {
145             Class JavaDoc rewriteRuleClass = Class.forName(className);
146             Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
147             parameterTypes[0] = Properties JavaDoc.class;
148             parameterTypes[1] = Properties JavaDoc.class;
149             Constructor JavaDoc rewriteRuleConstructor = rewriteRuleClass.getConstructor(parameterTypes);
150             Object JavaDoc[] initArgs = new Object JavaDoc[2];
151             initArgs[0] = originalFromRewrittenProps;
152             initArgs[1] = rewrittenFromOriginalProps;
153             RewriteRule rewriteRule = (RewriteRule) rewriteRuleConstructor.newInstance(initArgs);
154             rewriteRules.add(rewriteRule);
155             } catch (ClassNotFoundException JavaDoc cnfe) {
156                 logger.error("Couldn't find validator class " + className, cnfe);
157             } catch (IllegalAccessException JavaDoc iae) {
158                 logger.error("Error accessing validator class " + className, iae);
159             } catch (InstantiationException JavaDoc ie) {
160                 logger.error("Validation class "+className+" instantiation exception", ie);
161             } catch (InvocationTargetException JavaDoc ite) {
162                 logger.error("Invocation target exception while invoking validator class " + className, ite);
163             } catch (NoSuchMethodException JavaDoc nsme) {
164                 logger.error("No such method exception while creating instance of validator class "+className, nsme);
165             }
166             originalFromRewrittenProps = new Properties JavaDoc();
167             rewrittenFromOriginalProps = new Properties JavaDoc();
168             properties = new Properties JavaDoc();
169         }
170
171
172         final class AddPropertyRule extends Rule {
173
174             Properties JavaDoc properties = new Properties JavaDoc();
175             Properties JavaDoc targetProperties;
176
177             public AddPropertyRule(Properties JavaDoc targetProperties) {
178                 this.targetProperties = targetProperties;
179             }
180
181
182             public void begin(
183                 String JavaDoc namespace,
184                 String JavaDoc name,
185                 org.xml.sax.Attributes JavaDoc attributes)
186                 throws Exception JavaDoc {
187                 for (int i = 0; i < attributes.getLength(); i++) {
188                     properties.setProperty(
189                         attributes.getQName(i),
190                         attributes.getValue(i));
191                 }
192             }
193             public void end(String JavaDoc namespace, String JavaDoc name)
194                 throws Exception JavaDoc {
195                 targetProperties.setProperty(properties.getProperty("name"), properties.getProperty("value"));
196                 properties = new Properties JavaDoc();
197             }
198
199         }
200     }
201
202 }
Popular Tags