001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.builders;
011:
012: import java.util.*;
013:
014: import org.mmbase.core.event.NodeEvent;
015: import org.mmbase.module.core.*;
016: import org.mmbase.util.logging.*;
017:
018: /**
019: * @javadoc
020: * @author Daniel Ockeloen
021: * @version $Id: Versions.java,v 1.22 2007/12/06 08:03:47 michiel Exp $
022: */
023: public class Versions extends MMObjectBuilder implements MMBaseObserver {
024:
025: private static final Logger log = Logging
026: .getLoggerInstance(Versions.class);
027:
028: private Hashtable<String, Vector<VersionCacheNode>> cacheVersionHandlers = new Hashtable<String, Vector<VersionCacheNode>>();
029:
030: private Map<String, Integer> versionsCache = new Hashtable<String, Integer>();
031:
032: private boolean initialized = false;
033:
034: /**
035: * @javadoc
036: */
037: public boolean init() {
038: if (!initialized) {
039: super .init();
040: startCacheTypes();
041: List<MMObjectNode> versionNodes = getNodes();
042: for (MMObjectNode versionNode : versionNodes) {
043: String name = versionNode.getStringValue("name");
044: String type = versionNode.getStringValue("type");
045: Integer number = versionNode.getNumber();
046:
047: String key = type + "_" + name;
048: if (versionsCache.containsKey(key)) {
049: StringBuilder sb = new StringBuilder();
050: sb
051: .append("versions node[number,version,maintainer]:");
052: sb.append("[");
053: sb.append(versionNode.getNumber());
054: sb.append(",");
055: sb.append(versionNode.getIntValue("version"));
056: sb.append(",");
057: sb.append(versionNode.getStringValue("maintainer"));
058: sb.append("]");
059: log.warn("more than one version was found for "
060: + type + " with name " + name + " ."
061: + sb.toString());
062: } else {
063: versionsCache.put(key, number);
064: }
065: }
066:
067: initialized = true;
068: }
069:
070: return true;
071: }
072:
073: /**
074: * @param name the name of the component we want to get know the version information about
075: * @param type the type of tye component we want to get information about (application/builder)
076: * @return the node that contains version information about "name", "type" or null if no version
077: * information is avaiable
078: * @throws SearchQueryException
079: * @since MMBase-1.7
080: */
081: public MMObjectNode getVersionNode(String name, String type) {
082: MMObjectNode retval = null;
083:
084: String key = type + "_" + name;
085: if (versionsCache.containsKey(key)) {
086: Integer number = versionsCache.get(key);
087: retval = getNode(number.intValue());
088: }
089: return retval;
090: }
091:
092: /**
093: * @javadoc
094: */
095: public int getInstalledVersion(String name, String type) {
096: MMObjectNode node = getVersionNode(name, type);
097: if (node == null) {
098: return -1;
099: }
100: return node.getIntValue("version");
101: }
102:
103: /**
104: * @javadoc
105: */
106: public void setInstalledVersion(String name, String type,
107: String maintainer, int version) {
108:
109: MMObjectNode node = getVersionNode(name, type);
110: if (node == null) {
111: node = getNewNode("system");
112: node.setValue("name", name);
113: node.setValue("type", type);
114: node.setValue("maintainer", maintainer);
115: node.setValue("version", version);
116: int number = insert("system", node);
117:
118: String key = type + "_" + name;
119: versionsCache.put(key, number);
120: } else {
121: node.setValue("maintainer", maintainer);
122: node.setValue("version", version);
123: node.commit();
124: }
125: }
126:
127: /**
128: * @javadoc
129: */
130: public void updateInstalledVersion(String name, String type,
131: String maintainer, int version) {
132: setInstalledVersion(name, type, maintainer, version);
133: }
134:
135: /**
136: * @javadoc
137: */
138: public void startCacheTypes() {
139: // is there a CacheVersion file ?
140: String cacheversionfile = getInitParameter("cacheversionfile");
141:
142: if (cacheversionfile != null && !cacheversionfile.equals("")) {
143: VersionXMLCacheNodeReader parser = new VersionXMLCacheNodeReader(
144: cacheversionfile);
145: parser.setBuilder(this );
146: cacheVersionHandlers = parser
147: .getCacheVersions(cacheVersionHandlers);
148: }
149: for (Enumeration<String> e = cacheVersionHandlers.keys(); e
150: .hasMoreElements();) {
151: String bname = e.nextElement();
152: MMObjectBuilder builder = mmb.getBuilder(bname);
153: if (builder != null) {
154: builder.addLocalObserver(this );
155: builder.addRemoteObserver(this );
156: } else {
157: log.error("ERROR: Can't find builder : " + bname);
158: }
159: }
160: }
161:
162: /*
163: * (non-Javadoc)
164: * @see org.mmbase.module.core.MMObjectBuilder#notify(org.mmbase.core.event.NodeEvent)
165: */
166: public void notify(NodeEvent event) {
167: if (log.isDebugEnabled()) {
168: log.debug("Changed " + event.getMachine() + " "
169: + event.getNodeNumber() + " "
170: + event.getBuilderName() + " "
171: + NodeEvent.newTypeToOldType(event.getType()));
172: }
173: String builder = event.getBuilderName();
174: Vector<VersionCacheNode> subs = cacheVersionHandlers
175: .get(builder);
176: int inumber = event.getNodeNumber();
177: if (subs != null) {
178: for (VersionCacheNode cnode : subs) {
179: cnode.handleChanged(builder, inumber);
180: }
181: }
182: super.notify(event);
183: }
184: }
|