KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > 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.formmodel.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.ServiceSelector;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.cocoon.forms.CacheManager;
31 import org.apache.cocoon.forms.formmodel.WidgetDefinitionBuilder;
32 import org.apache.cocoon.forms.util.DomHelper;
33 import org.apache.excalibur.source.Source;
34 import org.apache.excalibur.source.SourceResolver;
35 import org.w3c.dom.Document JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37
38 /**
39  * @version $Id: LibraryManagerImpl.java 289538 2005-09-16 13:46:22Z sylvain $
40  *
41  */

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