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.core.schema;
018:
019: import java.io.File;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.romaframework.aspect.session.SessionInfo;
028: import org.romaframework.aspect.view.form.ContentComponent;
029: import org.romaframework.core.flow.Controller;
030: import org.romaframework.core.flow.ObjectContext;
031: import org.romaframework.core.resource.AutoReloadListener;
032: import org.romaframework.core.resource.AutoReloadManager;
033:
034: /**
035: * Resolve entity class and descriptors using the paths configured.
036: *
037: * @author Luca Garulli (luca.garulli@assetdata.it)
038: */
039: public class SchemaReloader implements AutoReloadListener {
040:
041: private final Map<File, List<String>> fileReloadingMapping = new HashMap<File, List<String>>();
042:
043: public final static String reloading_mutex = "reloading_mutex";
044:
045: private static Log log = LogFactory.getLog(SchemaReloader.class);
046:
047: /**
048: * Map the file to the entity name to be reloaded when updated.
049: *
050: * @param iFile
051: * @param iEntityName
052: */
053: public void addResourceForReloading(File iFile, String iEntityName) {
054: synchronized (fileReloadingMapping) {
055: List<String> resources = fileReloadingMapping.get(iFile);
056: if (resources == null) {
057: resources = new ArrayList<String>();
058: fileReloadingMapping.put(iFile, resources);
059:
060: // REGISTER DESCRIPTOR FILE TO BE WAKED UP ON RELOADING
061: ObjectContext.getInstance().getComponent(
062: AutoReloadManager.class).addResource(iFile,
063: this );
064: }
065: resources.add(iEntityName);
066: }
067: }
068:
069: public void signalUpdatedFile(File iFile) {
070: synchronized (reloading_mutex) {
071: List<String> resources = fileReloadingMapping.get(iFile);
072: if (resources == null)
073: return;
074:
075: log
076: .debug("[SchemaReloader.signalUpdatedFile] Caught file changing: "
077: + iFile
078: + ". Propagating the event to all listeners");
079:
080: SchemaClass classInfo = null;
081: for (String entity : resources) {
082: classInfo = ObjectContext.getInstance().getComponent(
083: SchemaManager.class).getClassInfo(entity);
084: classInfo.signalUpdatedFile(iFile);
085:
086: // OVERWRITE LIVING OBJECT SCHEMA OBJECTS BY COPYING NEW DEFINITION AND REFRESH ITS
087: Map<SessionInfo, ContentComponent> forms = ObjectContext
088: .getInstance().getFormsByClass(
089: classInfo.getClazz());
090: for (Map.Entry<SessionInfo, ContentComponent> entry : forms
091: .entrySet()) {
092: entry.getValue().getSchemaInstance()
093: .copyDefinition(classInfo);
094: ObjectContext.getInstance().refresh(entry.getKey(),
095: entry.getValue());
096: }
097: }
098:
099: if (classInfo != null)
100: // FLUSH ALL OBJECTS INSIDE SESSIONS
101: Controller.getInstance().flushObjectsOfClass(
102: classInfo.getName());
103: }
104: }
105: }
|