001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.rpad;
018:
019: import java.lang.reflect.Method;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Locale;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.xml.sax.Attributes;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.helpers.DefaultHandler;
030:
031: public class RepositoryConfigHandler extends DefaultHandler {
032: /**
033: * Logger for this class
034: */
035: private static final Log log = LogFactory
036: .getLog(RepositoryConfigHandler.class);
037:
038: private String className;
039:
040: private String propertyName;
041:
042: private String repositoryName;
043:
044: private Map properties;
045:
046: //TODO
047: // private String typeName;
048: // private Map types;
049:
050: private String currentQName;
051:
052: private Map repositories;
053:
054: public RepositoryConfigHandler() {
055: className = null;
056: propertyName = null;
057: properties = new HashMap();
058: currentQName = null;
059: repositories = new HashMap();
060: }
061:
062: protected Repository loadRepository(String className, Map properties) {
063: try {
064: Class cls = Class.forName(className);
065: Object obj = cls.newInstance();
066: if (obj instanceof Repository) {
067: Repository repo = (Repository) obj;
068: for (Iterator i = properties.entrySet().iterator(); i
069: .hasNext();) {
070: try {
071: Map.Entry entry = (Map.Entry) i.next();
072: String propertyName = (String) entry.getKey();
073: Class[] clsArgs = new Class[1];
074: //TODO set type
075: clsArgs[0] = String.class;
076: Method method = cls.getMethod("set"
077: + propertyName.substring(0, 1)
078: .toUpperCase(Locale.ENGLISH)
079: + propertyName.substring(1), clsArgs);
080: Object[] args = new Object[1];
081: args[0] = entry.getValue();
082: method.invoke(repo, args);
083: } catch (Exception e) {
084: log.error(
085: "Could invoke a method for property: "
086: + propertyName, e);
087: }
088: }
089:
090: try {
091: // call init()
092: Method initMethod = cls.getMethod("init", null);
093: initMethod.invoke(repo, null);
094: } catch (Exception e) {
095: log.error("Could not initialize an instance: "
096: + className, e);
097: repo.setAvailable(false);
098: }
099:
100: return repo;
101: }
102: } catch (Exception e) {
103: log.error("Could not create an instance: " + className, e);
104: }
105: return null;
106: }
107:
108: /* (non-Javadoc)
109: * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
110: */
111: public void endElement(String uri, String localName, String qName)
112: throws SAXException {
113: if ("repository".equals(qName)) {
114: if (className != null && repositoryName != null) {
115: Repository repo = loadRepository(className, properties);
116: if (repo != null) {
117: repositories.put(repositoryName, repo);
118: } else {
119: log.warn("Could not load " + className);
120: }
121: } else {
122: log.warn("The class name or repository name are null.");
123: }
124: }
125:
126: if (currentQName != null && currentQName.equals(qName)) {
127: currentQName = null;
128: }
129: }
130:
131: /* (non-Javadoc)
132: * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
133: */
134: public void startElement(String uri, String localName,
135: String qName, Attributes attributes) throws SAXException {
136: currentQName = qName;
137:
138: if ("repository".equals(qName)) {
139: className = null;
140: propertyName = null;
141: properties = new HashMap();
142: } else if ("class".equals(qName)) {
143: className = attributes.getValue("name");
144: } else if ("property".equals(qName)) {
145: propertyName = attributes.getValue("name");
146: }
147: }
148:
149: /* (non-Javadoc)
150: * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
151: */
152: public void characters(char[] ch, int start, int length)
153: throws SAXException {
154:
155: if ("property".equals(currentQName)) {
156: if (propertyName != null) {
157: properties.put(propertyName, new String(ch, start,
158: length));
159: }
160: propertyName = null;
161: } else if ("name".equals(currentQName)) {
162: repositoryName = new String(ch, start, length);
163: properties.put("name", repositoryName);
164: }
165: }
166:
167: /**
168: * @return the repositories
169: */
170: public Map getRepositories() {
171: return repositories;
172: }
173:
174: }
|