001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.cluster;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.util.HashMap;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.springframework.beans.BeansException;
029: import org.springframework.beans.factory.BeanFactory;
030: import org.springframework.beans.factory.BeanFactoryAware;
031:
032: /**
033: * Node Manager
034: *
035: * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
036: * @version
037: */
038: public class NodeManagerImpl implements NodeManager, BeanFactoryAware {
039: protected final static Log log = LogFactory
040: .getLog(NodeManagerImpl.class);
041:
042: /**
043: * added support for bean factory to create profile rules
044: */
045: private BeanFactory beanFactory;
046:
047: private HashMap nodes = null;
048: private File rootIndexDir = null;
049:
050: /** the default criterion bean name */
051: private String nodeInformationBean = "NodeInformation";
052:
053: public NodeManagerImpl(String indexRoot, String nodeInformationBean)
054: throws Exception {
055:
056: //assume it's full path for now
057: rootIndexDir = new File(indexRoot);
058: this .nodeInformationBean = nodeInformationBean;
059:
060: if (!(rootIndexDir.exists()))
061: rootIndexDir.mkdirs();
062: load();
063: }
064:
065: protected void save() {
066: try {
067: FileOutputStream fout = new FileOutputStream(rootIndexDir
068: .getAbsolutePath()
069: + "/nodeInfo.ser");
070: ObjectOutputStream oos = new ObjectOutputStream(fout);
071: oos.writeObject(nodes);
072: oos.close();
073: } catch (Exception e) {
074: log.error("Failed to write nodes data file to "
075: + rootIndexDir.getAbsolutePath() + "/nodeInfo.ser"
076: + " - error : " + e.getLocalizedMessage());
077: e.printStackTrace();
078: }
079: }
080:
081: protected void load() {
082: File data = new File(rootIndexDir.getAbsolutePath()
083: + "/nodeInfo.ser");
084: if (data.exists()) {
085: try {
086: FileInputStream fin = new FileInputStream(data
087: .getAbsolutePath());
088: ObjectInputStream ois = new ObjectInputStream(fin);
089: nodes = (HashMap) ois.readObject();
090: ois.close();
091: } catch (Exception e) {
092: log.error("Failed to read nodes data file from "
093: + data.getAbsolutePath() + " - error : "
094: + e.getLocalizedMessage());
095: nodes = new HashMap();
096: }
097: } else {
098: try {
099: data.createNewFile();
100: } catch (Exception e) {
101: log
102: .error("Failed to create new nodes data file error : "
103: + e.getLocalizedMessage());
104: e.printStackTrace();
105: }
106: nodes = new HashMap();
107: }
108:
109: // NodeInformationImpl temp = new NodeInformationImpl();
110: // temp.setContextName("tttt");
111: }
112:
113: public int checkNode(Long id, String contextName) {
114: if ((contextName == null) || (id == null))
115: return NodeManager.INVALID_NODE_REQUEST;
116: NodeInformation info = (NodeInformation) nodes.get(contextName);
117: if (info == null)
118: return NodeManager.NODE_NEW;
119: if (info.getId().longValue() < id.longValue())
120: return NodeManager.NODE_OUTDATED;
121: return NodeManager.NODE_SAVED;
122: }
123:
124: public void addNode(Long id, String contextName) throws Exception {
125: if ((contextName == null) || (id == null))
126: return;
127: NodeInformation info = (NodeInformation) nodes.get(contextName);
128: if (info == null) {
129: info = createNodeInformation();
130: info.setContextName(contextName);
131: }
132: info.setId(id);
133: nodes.put(contextName, info);
134: save();
135: }
136:
137: public void removeNode(String contextName) throws Exception {
138: if (contextName == null)
139: return;
140: NodeInformation info = (NodeInformation) nodes.get(contextName);
141: if (info == null)
142: return;
143: nodes.remove(contextName);
144: save();
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see org.apache.jetspeed.profiler.Profiler#createRuleCriterion()
151: */
152: protected NodeInformation createNodeInformation()
153: throws ClassNotFoundException {
154: try {
155: NodeInformation nodeInformation = (NodeInformation) beanFactory
156: .getBean(this .nodeInformationBean,
157: NodeInformation.class);
158: return nodeInformation;
159: } catch (Exception e) {
160: log.error("Failed to create nodeInformation for "
161: + nodeInformationBean + " error : "
162: + e.getLocalizedMessage());
163: throw new ClassNotFoundException(
164: "Spring failed to create the "
165: + " nodeInformation bean.", e);
166: }
167:
168: }
169:
170: /*
171: * Method called automatically by Spring container upon initialization
172: *
173: * @param beanFactory automatically provided by framework @throws
174: * BeansException
175: */
176: public void setBeanFactory(BeanFactory beanFactory)
177: throws BeansException {
178: this .beanFactory = beanFactory;
179: }
180:
181: public int getNumberOfNodes() {
182: return nodes.size();
183: }
184:
185: }
|