KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > binding > library > LibraryManagerImpl


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.binding.library;
17
18 import org.apache.avalon.framework.CascadingException;
19 import org.apache.avalon.framework.activity.Disposable;
20 import org.apache.avalon.framework.activity.Initializable;
21 import org.apache.avalon.framework.configuration.Configurable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.avalon.framework.service.Serviceable;
28 import org.apache.avalon.framework.thread.ThreadSafe;
29 import org.apache.cocoon.forms.CacheManager;
30 import org.apache.cocoon.forms.binding.JXPathBindingManager;
31 import org.apache.cocoon.forms.util.DomHelper;
32 import org.apache.excalibur.source.Source;
33 import org.apache.excalibur.source.SourceResolver;
34 import org.w3c.dom.Document JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36
37 /**
38  * @version $Id: LibraryManagerImpl.java 289538 2005-09-16 13:46:22Z sylvain $
39  *
40  */

41 public class LibraryManagerImpl extends AbstractLogEnabled implements LibraryManager, ThreadSafe, Serviceable,
42 Configurable, Initializable, Disposable {
43
44     protected static final String JavaDoc PREFIX = "CocoonFormBindingLibrary:";
45     
46     private ServiceManager serviceManager;
47     private Configuration configuration;
48     private CacheManager cacheManager;
49     
50     private JXPathBindingManager bindingManager;
51
52     public void configure(Configuration configuration) throws ConfigurationException {
53         this.configuration = configuration;
54         getLogger().debug("Gotten a config: top level element: "+this.configuration);
55     }
56
57     public void service(ServiceManager serviceManager) throws ServiceException {
58         this.serviceManager = serviceManager;
59         this.cacheManager = (CacheManager)serviceManager.lookup(CacheManager.ROLE);
60     }
61     
62     public void setBindingManager(JXPathBindingManager bindingManager) {
63         this.bindingManager = bindingManager;
64     }
65     
66     public void initialize() throws Exception JavaDoc {
67         
68         // read config to "preload" libraries
69
}
70     
71     public boolean libraryInCache(String JavaDoc librarysource) throws Exception JavaDoc {
72         return libraryInCache(librarysource,null);
73     }
74     
75     public boolean libraryInCache(String JavaDoc librarysource, String JavaDoc relative) throws Exception JavaDoc {
76         SourceResolver sourceResolver = null;
77         Source source = null;
78         
79         if(getLogger().isDebugEnabled())
80             getLogger().debug("Checking if library is in cache: '"+librarysource+"' relative to '"+relative+"'");
81
82         Library lib = null;
83         boolean result = false;
84         
85         try {
86             sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
87             source = sourceResolver.resolveURI(librarysource, relative, null);
88             
89             lib = (Library)this.cacheManager.get(source, PREFIX);
90             
91             if( lib != null && lib.dependenciesHaveChanged() ) {
92                 result = false;
93                 this.cacheManager.set(null,source,PREFIX); //evict?
94
}
95             else if( lib == null )
96                 result = false;
97             else
98                 result = true;
99         } catch(Exception JavaDoc e) {
100             if(getLogger().isErrorEnabled())
101                 getLogger().error("Problem getting library '"+librarysource+"' relative to '"+relative+"'!",e);
102             throw e;
103         } finally {
104             if (source != null)
105                 sourceResolver.release(source);
106             if (sourceResolver != null)
107                 serviceManager.release(sourceResolver);
108         }
109
110         if(getLogger().isDebugEnabled()) {
111             if(result)
112                 getLogger().debug("Library IS in cache : '"+librarysource+"' relative to '"+relative+"'");
113             else
114                 getLogger().debug("Library IS NOT in cache : '"+librarysource+"' relative to '"+relative+"'");
115         }
116         
117         return result;
118     }
119     
120     public Library getLibrary(String JavaDoc librarysource) throws Exception JavaDoc {
121         return getLibrary(librarysource,null);
122     }
123     
124     public Library getLibrary(String JavaDoc librarysource, String JavaDoc relative) throws Exception JavaDoc {
125         SourceResolver sourceResolver = null;
126         Source source = null;
127         Document JavaDoc libraryDocument = null;
128
129         Library lib = null;
130         
131         if(getLogger().isDebugEnabled())
132             getLogger().debug("Getting library instance: '"+librarysource+"' relative to '"+relative+"'");
133         
134         try {
135             sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
136             source = sourceResolver.resolveURI(librarysource, relative, null);
137             
138             lib = (Library)this.cacheManager.get(source, PREFIX);
139             
140             if( lib != null && lib.dependenciesHaveChanged() ) {
141                 if(getLogger().isDebugEnabled())
142                     getLogger().debug("Library dependencies changed, invalidating!");
143                 
144                 lib = null;
145             }
146             
147             if( lib == null ) {
148                 if(getLogger().isDebugEnabled())
149                     getLogger().debug("Library not in cache, creating!");
150                 
151                 try {
152                     InputSource JavaDoc inputSource = new InputSource JavaDoc(source.getInputStream());
153                     inputSource.setSystemId(source.getURI());
154                     libraryDocument = DomHelper.parse(inputSource, this.serviceManager);
155                     
156                     lib = getNewLibrary();
157                     lib.buildLibrary(libraryDocument.getDocumentElement());
158                     
159                     this.cacheManager.set(lib,source,PREFIX);
160                     
161                 } catch (Exception JavaDoc e) {
162                     throw new CascadingException("Could not parse form definition from " +
163                                                  source.getURI(), e);
164                 }
165             }
166         } finally {
167             if (source != null)
168                 sourceResolver.release(source);
169             if (sourceResolver != null)
170                 serviceManager.release(sourceResolver);
171         }
172
173         return lib;
174     }
175     
176     public Library getNewLibrary() {
177         Library lib = new Library(this);
178         lib.setAssistant(this.bindingManager.getBuilderAssistant());
179         
180         if(getLogger().isDebugEnabled())
181             getLogger().debug("Created new library! "+lib);
182         
183         return lib;
184     }
185
186     public void dispose() {
187         this.serviceManager.release(this.cacheManager);
188         this.cacheManager = null;
189         this.serviceManager = null;
190     }
191     
192     public void debug(String JavaDoc msg) {
193         if(getLogger().isDebugEnabled()) {
194             getLogger().debug(msg);
195         }
196     }
197     
198 }
199
Popular Tags