001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.jspf.impl;
019:
020: import org.apache.james.jspf.core.LogEnabled;
021: import org.apache.james.jspf.core.Logger;
022: import org.apache.james.jspf.core.exceptions.PermErrorException;
023: import org.apache.james.jspf.parser.TermsFactory;
024: import org.apache.james.jspf.terms.Configuration;
025: import org.apache.james.jspf.terms.ConfigurationEnabled;
026: import org.apache.james.jspf.wiring.WiringService;
027: import org.apache.james.jspf.wiring.WiringServiceException;
028: import org.apache.james.jspf.wiring.WiringServiceTable;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Collections;
035: import java.util.Properties;
036:
037: /**
038: * The default implementation of the TermsFactory
039: */
040: public class DefaultTermsFactory implements TermsFactory {
041:
042: private String termFile = "org/apache/james/jspf/parser/jspf.default.terms";
043:
044: private Collection mechanismsCollection;
045:
046: private Collection modifiersCollection;
047:
048: private Logger log;
049:
050: private WiringService wiringService;
051:
052: public DefaultTermsFactory(Logger log) {
053: this .log = log;
054: this .wiringService = new WiringServiceTable();
055: ((WiringServiceTable) this .wiringService).put(LogEnabled.class,
056: log);
057: init();
058: }
059:
060: public DefaultTermsFactory(Logger log, WiringService wiringService) {
061: this .log = log;
062: this .wiringService = wiringService;
063: init();
064: }
065:
066: /**
067: * Initialize the factory and the services
068: */
069: private void init() {
070: try {
071: InputStream is = Thread.currentThread()
072: .getContextClassLoader().getResourceAsStream(
073: termFile);
074: Properties p = new Properties();
075: p.load(is);
076: String mechs = p.getProperty("mechanisms");
077: String mods = p.getProperty("modifiers");
078: String[] classes;
079: classes = mechs.split(",");
080: Class[] knownMechanisms = new Class[classes.length];
081: for (int i = 0; i < classes.length; i++) {
082: log.debug("Add following class as known mechanismn: "
083: + classes[i]);
084: knownMechanisms[i] = Thread.currentThread()
085: .getContextClassLoader().loadClass(classes[i]);
086: }
087: mechanismsCollection = createTermDefinitionCollection(knownMechanisms);
088: classes = mods.split(",");
089: Class[] knownModifiers = new Class[classes.length];
090: for (int i = 0; i < classes.length; i++) {
091: log.debug("Add following class as known modifier: "
092: + classes[i]);
093: knownModifiers[i] = Thread.currentThread()
094: .getContextClassLoader().loadClass(classes[i]);
095: }
096: modifiersCollection = createTermDefinitionCollection(knownModifiers);
097:
098: } catch (IOException e) {
099: throw new IllegalStateException(
100: "Term configuration cannot be found");
101: } catch (ClassNotFoundException e) {
102: throw new IllegalStateException(
103: "One configured class cannot be found");
104: }
105: }
106:
107: /**
108: * Create a collection of term definitions supported by this factory.
109: *
110: * @param classes
111: * classes to analyze
112: * @param staticFieldName
113: * static field to concatenate
114: * @return map <Class,Pattern>
115: */
116: private Collection createTermDefinitionCollection(Class[] classes) {
117: Collection l = new ArrayList();
118: for (int j = 0; j < classes.length; j++) {
119: try {
120: l.add(new DefaultTermDefinition(classes[j]));
121: } catch (Exception e) {
122: log.debug("Unable to create the term collection", e);
123: throw new IllegalStateException(
124: "Unable to create the term collection");
125: }
126: }
127: return Collections.synchronizedCollection(l);
128: }
129:
130: /**
131: * @see org.apache.james.jspf.parser.TermsFactory#createTerm(java.lang.Class, org.apache.james.jspf.terms.Configuration)
132: */
133: public Object createTerm(Class termDef, Configuration subres)
134: throws PermErrorException, InstantiationException {
135: try {
136: Object term = termDef.newInstance();
137:
138: try {
139: wiringService.wire(term);
140: } catch (WiringServiceException e) {
141: throw new InstantiationException(
142: "Unexpected error adding dependencies to term: "
143: + e.getMessage());
144: }
145:
146: if (term instanceof ConfigurationEnabled) {
147: if (subres == null || subres.groupCount() == 0) {
148: ((ConfigurationEnabled) term).config(null);
149: } else {
150: ((ConfigurationEnabled) term).config(subres);
151: }
152: }
153: return term;
154: } catch (IllegalAccessException e) {
155: throw new InstantiationException(
156: "Unexpected error creating term: " + e.getMessage());
157: }
158: }
159:
160: /**
161: * @see org.apache.james.jspf.parser.TermsFactory#getMechanismsCollection()
162: */
163: public Collection getMechanismsCollection() {
164: return mechanismsCollection;
165: }
166:
167: /**
168: * @see org.apache.james.jspf.parser.TermsFactory#getModifiersCollection()
169: */
170: public Collection getModifiersCollection() {
171: return modifiersCollection;
172: }
173:
174: }
|