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.cocoon.forms.binding.library;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.component.Component;
021: import org.apache.avalon.framework.configuration.Configurable;
022: import org.apache.avalon.framework.configuration.Configuration;
023: import org.apache.avalon.framework.configuration.ConfigurationException;
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.service.Serviceable;
028: import org.apache.avalon.framework.thread.ThreadSafe;
029: import org.apache.excalibur.source.Source;
030: import org.apache.excalibur.source.SourceResolver;
031:
032: import org.apache.cocoon.forms.CacheManager;
033: import org.apache.cocoon.forms.binding.JXPathBindingManager;
034: import org.apache.cocoon.forms.util.DomHelper;
035: import org.apache.cocoon.util.location.LocationImpl;
036:
037: import org.w3c.dom.Document;
038: import org.xml.sax.InputSource;
039:
040: /**
041: * @version $Id: LibraryManagerImpl.java 517733 2007-03-13 15:37:22Z vgritsenko $
042: */
043: public class LibraryManagerImpl extends AbstractLogEnabled implements
044: LibraryManager, Serviceable, Configurable, Disposable,
045: ThreadSafe, Component {
046:
047: protected static final String PREFIX = "CocoonFormBindingLibrary:";
048:
049: private ServiceManager manager;
050: private CacheManager cacheManager;
051:
052: private JXPathBindingManager bindingManager;
053:
054: public void configure(Configuration configuration)
055: throws ConfigurationException {
056: // TODO Read config to "preload" libraries
057: }
058:
059: public void service(ServiceManager serviceManager)
060: throws ServiceException {
061: this .manager = serviceManager;
062: this .cacheManager = (CacheManager) serviceManager
063: .lookup(CacheManager.ROLE);
064: }
065:
066: public void setBindingManager(JXPathBindingManager bindingManager) {
067: this .bindingManager = bindingManager;
068: }
069:
070: public Library get(String sourceURI) throws LibraryException {
071: return get(sourceURI, null);
072: }
073:
074: public Library get(String sourceURI, String baseURI)
075: throws LibraryException {
076: SourceResolver sourceResolver = null;
077: Source source = null;
078: try {
079: try {
080: sourceResolver = (SourceResolver) manager
081: .lookup(SourceResolver.ROLE);
082: source = sourceResolver.resolveURI(sourceURI, baseURI,
083: null);
084: } catch (Exception e) {
085: throw new LibraryException(
086: "Unable to resolve library.", e,
087: new LocationImpl("[LibraryManager]", sourceURI));
088: }
089:
090: Library lib = (Library) this .cacheManager.get(source,
091: PREFIX);
092: if (lib != null && lib.dependenciesHaveChanged()) {
093: if (getLogger().isDebugEnabled()) {
094: getLogger().debug(
095: "Library IS REMOVED from cache: '"
096: + sourceURI + "' relative to '"
097: + baseURI + "'");
098: }
099: this .cacheManager.remove(source, PREFIX); // evict?
100: return null;
101: }
102:
103: if (getLogger().isDebugEnabled()) {
104: if (lib != null) {
105: getLogger()
106: .debug(
107: "Library IS in cache: '"
108: + sourceURI
109: + "' relative to '"
110: + baseURI + "'");
111: } else {
112: getLogger()
113: .debug(
114: "Library IS NOT in cache: '"
115: + sourceURI
116: + "' relative to '"
117: + baseURI + "'");
118: }
119: }
120:
121: return lib;
122: } finally {
123: if (source != null) {
124: sourceResolver.release(source);
125: }
126: if (sourceResolver != null) {
127: manager.release(sourceResolver);
128: }
129: }
130: }
131:
132: public Library load(String sourceURI) throws LibraryException {
133: return load(sourceURI, null);
134: }
135:
136: public Library load(String sourceURI, String baseURI)
137: throws LibraryException {
138: SourceResolver sourceResolver = null;
139: Source source = null;
140:
141: if (getLogger().isDebugEnabled()) {
142: getLogger().debug(
143: "Loading library: '" + sourceURI
144: + "' relative to '" + baseURI + "'");
145: }
146:
147: try {
148: try {
149: sourceResolver = (SourceResolver) manager
150: .lookup(SourceResolver.ROLE);
151: source = sourceResolver.resolveURI(sourceURI, baseURI,
152: null);
153: } catch (Exception e) {
154: throw new LibraryException(
155: "Unable to resolve library.", e,
156: new LocationImpl("[LibraryManager]", sourceURI));
157: }
158:
159: Library lib = (Library) this .cacheManager.get(source,
160: PREFIX);
161: if (lib != null && lib.dependenciesHaveChanged()) {
162: if (getLogger().isDebugEnabled()) {
163: getLogger().debug(
164: "Library IS EXPIRED in cache: '"
165: + sourceURI + "' relative to '"
166: + baseURI + "'");
167: }
168: lib = null;
169: }
170:
171: if (lib == null) {
172: if (getLogger().isDebugEnabled()) {
173: getLogger().debug(
174: "Library IS NOT in cache, loading: '"
175: + sourceURI + "' relative to '"
176: + baseURI + "'");
177: }
178:
179: try {
180: InputSource inputSource = new InputSource(source
181: .getInputStream());
182: inputSource.setSystemId(source.getURI());
183:
184: Document doc = DomHelper.parse(inputSource,
185: this .manager);
186: lib = newLibrary();
187: lib.buildLibrary(doc.getDocumentElement());
188:
189: this .cacheManager.set(lib, source, PREFIX);
190: } catch (Exception e) {
191: throw new LibraryException(
192: "Unable to load library.", e,
193: new LocationImpl("[LibraryManager]", source
194: .getURI()));
195: }
196: }
197:
198: return lib;
199: } finally {
200: if (source != null) {
201: sourceResolver.release(source);
202: }
203: if (sourceResolver != null) {
204: manager.release(sourceResolver);
205: }
206: }
207: }
208:
209: public Library newLibrary() {
210: Library lib = new Library(this , bindingManager
211: .getBuilderAssistant());
212: lib.enableLogging(getLogger());
213: if (getLogger().isDebugEnabled()) {
214: getLogger().debug("Created new library! " + lib);
215: }
216:
217: return lib;
218: }
219:
220: public void dispose() {
221: this .manager.release(this .cacheManager);
222: this .cacheManager = null;
223: this .manager = null;
224: }
225:
226: public void debug(String msg) {
227: if (getLogger().isDebugEnabled()) {
228: getLogger().debug(msg);
229: }
230: }
231: }
|