View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.portlets.rpad;
18  
19  import java.lang.reflect.Method;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Locale;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.xml.sax.Attributes;
28  import org.xml.sax.SAXException;
29  import org.xml.sax.helpers.DefaultHandler;
30  
31  public class RepositoryConfigHandler extends DefaultHandler
32  {
33      /***
34       * Logger for this class
35       */
36      private static final Log log = LogFactory
37              .getLog(RepositoryConfigHandler.class);
38  
39      private String className;
40  
41      private String propertyName;
42  
43      private String repositoryName;
44  
45      private Map properties;
46  
47      //TODO
48      //    private String typeName;
49      //    private Map types;
50  
51      private String currentQName;
52  
53      private Map repositories;
54  
55      public RepositoryConfigHandler()
56      {
57          className = null;
58          propertyName = null;
59          properties = new HashMap();
60          currentQName = null;
61          repositories = new HashMap();
62      }
63  
64      protected Repository loadRepository(String className, Map properties)
65      {
66          try
67          {
68              Class cls = Class.forName(className);
69              Object obj = cls.newInstance();
70              if (obj instanceof Repository)
71              {
72                  Repository repo = (Repository) obj;
73                  for (Iterator i = properties.entrySet().iterator(); i.hasNext();)
74                  {
75                      try
76                      {
77                          Map.Entry entry = (Map.Entry) i.next();
78                          String propertyName = (String) entry.getKey();
79                          Class[] clsArgs = new Class[1];
80                          //TODO set type
81                          clsArgs[0] = String.class;
82                          Method method = cls.getMethod("set"
83                                  + propertyName.substring(0, 1).toUpperCase(
84                                          Locale.ENGLISH)
85                                  + propertyName.substring(1), clsArgs);
86                          Object[] args = new Object[1];
87                          args[0] = entry.getValue();
88                          method.invoke(repo, args);
89                      }
90                      catch (Exception e)
91                      {
92                          log.error("Could invoke a method for property: "
93                                  + propertyName, e);
94                      }
95                  }
96  
97                  try
98                  {
99                      // call init()
100                     Method initMethod = cls.getMethod("init", null);
101                     initMethod.invoke(repo, null);
102                 }
103                 catch (Exception e)
104                 {
105                     log.error("Could not initialize an instance: " + className,
106                             e);
107                     repo.setAvailable(false);
108                 }
109 
110                 return repo;
111             }
112         }
113         catch (Exception e)
114         {
115             log.error("Could not create an instance: " + className, e);
116         }
117         return null;
118     }
119 
120     /* (non-Javadoc)
121      * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
122      */
123     public void endElement(String uri, String localName, String qName)
124             throws SAXException
125     {
126         if ("repository".equals(qName))
127         {
128             if (className != null && repositoryName != null)
129             {
130                 Repository repo = loadRepository(className, properties);
131                 if (repo != null)
132                 {
133                     repositories.put(repositoryName, repo);
134                 }
135                 else
136                 {
137                     log.warn("Could not load " + className);
138                 }
139             }
140             else
141             {
142                 log.warn("The class name or repository name are null.");
143             }
144         }
145 
146         if (currentQName != null && currentQName.equals(qName))
147         {
148             currentQName = null;
149         }
150     }
151 
152     /* (non-Javadoc)
153      * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
154      */
155     public void startElement(String uri, String localName, String qName,
156             Attributes attributes) throws SAXException
157     {
158         currentQName = qName;
159 
160         if ("repository".equals(qName))
161         {
162             className = null;
163             propertyName = null;
164             properties = new HashMap();
165         }
166         else if ("class".equals(qName))
167         {
168             className = attributes.getValue("name");
169         }
170         else if ("property".equals(qName))
171         {
172             propertyName = attributes.getValue("name");
173         }
174     }
175 
176     /* (non-Javadoc)
177      * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
178      */
179     public void characters(char[] ch, int start, int length)
180             throws SAXException
181     {
182 
183         if ("property".equals(currentQName))
184         {
185             if (propertyName != null)
186             {
187                 properties.put(propertyName, new String(ch, start, length));
188             }
189             propertyName = null;
190         }
191         else if ("name".equals(currentQName))
192         {
193             repositoryName = new String(ch, start, length);
194             properties.put("name", repositoryName);
195         }
196     }
197 
198     /***
199      * @return the repositories
200      */
201     public Map getRepositories()
202     {
203         return repositories;
204     }
205 
206 }