001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.aspect.view.screen.config;
018:
019: import java.io.File;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.romaframework.core.config.Configurable;
024: import org.romaframework.core.config.RomaApplicationContext;
025: import org.romaframework.core.resource.AutoReloadListener;
026: import org.romaframework.core.resource.AutoReloadManager;
027: import org.romaframework.core.schema.SchemaClassResolver;
028:
029: /**
030: * Handle Desktop configuration descriptor loading.
031: *
032: * @author Luca Garulli (luca.garulli@assetdata.it)
033: */
034: public class ScreenConfigurationLoader extends
035: Configurable<ScreenConfiguration> implements AutoReloadListener {
036: public static final String DEF_DESKTOP_FILE_NAME = "main-screen.xml";
037: private static Log log = LogFactory
038: .getLog(ScreenConfigurationLoader.class);
039:
040: /**
041: * Get a Desktop Descriptor. Load if necessary and cache if for further
042: * accesses.
043: *
044: * @param iName
045: * @return
046: */
047: public ScreenConfiguration getDescriptor(String iName) {
048: ScreenConfiguration descr;
049:
050: synchronized (this ) {
051: descr = getConfiguration(iName);
052: if (descr == null) {
053: // LOAD FROM DESCRIPTOR
054: descr = loadDescriptor(iName);
055: if (descr != null) {
056: addConfiguration(iName, descr);
057:
058: // REGISTER DESCRIPTOR FILE TO BE WAKED UP ON RELOADING
059: RomaApplicationContext.getInstance().getBean(
060: AutoReloadManager.class).addResource(
061: descr.getFile(), this );
062: }
063: }
064: }
065: return descr;
066: }
067:
068: /**
069: * Load XML descriptor using Apache XMLBeans. Entities are cached.
070: *
071: * @param iEntityName
072: * @return Entity instance read from XML descriptor
073: */
074: protected ScreenConfiguration loadDescriptor(String iEntityName) {
075: ScreenConfiguration descr = null;
076: String filePath = "<unsetted>";
077: try {
078: filePath = RomaApplicationContext.getInstance().getBean(
079: SchemaClassResolver.class).getEntityDescriptor(
080: iEntityName);
081:
082: if (filePath != null) {
083: File location = new File(getClass().getResource(
084: filePath).toURI());
085: log
086: .debug("[DesktopConfigurationLoader.loadDescriptor] Loading desktop from: "
087: + location.getAbsolutePath());
088: descr = new ScreenConfiguration(location);
089: }
090: } catch (Exception e) {
091: log.error(
092: "[DesktopConfigurationLoader.loadDescriptor] Error on desktop resource: "
093: + filePath, e);
094: }
095: return descr;
096: }
097:
098: /**
099: * Reload desktop configuration from file. This event is invoked when the file
100: * descriptor is changed.
101: */
102: public void signalUpdatedFile(File iFile) {
103: synchronized (this ) {
104: for (ScreenConfiguration d : configuration.values()) {
105: if (d.getFile().equals(iFile)) {
106: log
107: .warn("[DesktopConfigurationLoader.signalUpdatedFile] Reloading file: "
108: + iFile);
109:
110: try {
111: d.load();
112: } catch (Exception e) {
113: log
114: .error("[DesktopConfigurationLoader.signalUpdatedFile] Error on loading updated descriptor: "
115: + iFile.getAbsolutePath());
116: }
117: break;
118: }
119: }
120: }
121: }
122: }
|