View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.cluster;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.util.HashMap;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.springframework.beans.BeansException;
29  import org.springframework.beans.factory.BeanFactory;
30  import org.springframework.beans.factory.BeanFactoryAware;
31  
32  /***
33   * Node Manager 
34   *
35   * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
36   * @version 
37   */
38  public class NodeManagerImpl implements NodeManager,BeanFactoryAware
39  {
40  	protected final static Log log = LogFactory.getLog(NodeManagerImpl.class);
41  
42      /***
43       * added support for bean factory to create profile rules
44       */
45      private BeanFactory beanFactory;
46  
47  	private HashMap nodes = null; 
48  	private File rootIndexDir = null;
49  	    
50      /*** the default criterion bean name */
51      private String nodeInformationBean = "NodeInformation";
52  
53  	
54  	public NodeManagerImpl(String indexRoot, String nodeInformationBean)
55      throws Exception
56      {
57      	
58          //assume it's full path for now
59          rootIndexDir = new File(indexRoot);
60          this.nodeInformationBean = nodeInformationBean;
61  
62          if (!(rootIndexDir.exists()))
63          	rootIndexDir.mkdirs();
64          load();
65      }
66  
67  	
68  	protected void save()
69  	{
70  		try {
71  		      FileOutputStream fout = new FileOutputStream(rootIndexDir.getAbsolutePath()+ "/nodeInfo.ser");
72  		      ObjectOutputStream oos = new ObjectOutputStream(fout);
73  		      oos.writeObject(nodes);
74  		      oos.close();
75  		      }
76  		   catch (Exception e) 
77  		   {
78  	            log.error("Failed to write nodes data file to  " + rootIndexDir.getAbsolutePath()+ "/nodeInfo.ser" + " - error : " + e.getLocalizedMessage());
79  			   e.printStackTrace(); 
80  			}
81  	}
82      
83  	protected void load()
84  	{
85  			File data = new File( rootIndexDir.getAbsolutePath()+ "/nodeInfo.ser");
86  			if (data.exists())
87  			{
88  				try {
89  				    FileInputStream fin = new FileInputStream(data.getAbsolutePath());
90  				    ObjectInputStream ois = new ObjectInputStream(fin);
91  				    nodes = (HashMap) ois.readObject();
92  				    ois.close();
93  				    }
94  				   catch (Exception e) 
95  				   { 
96  			            log.error("Failed to read nodes data file from " + data.getAbsolutePath() + " - error : " + e.getLocalizedMessage());
97  					   nodes = new HashMap();
98  				   }
99  			}
100 			else
101 			{
102 				try
103 				{
104 					data.createNewFile();
105 				}
106 				catch (Exception e)
107 				{
108 		            log.error("Failed to create new nodes data file error : " + e.getLocalizedMessage());
109 					e.printStackTrace();
110 				}
111 				nodes = new HashMap();
112 			}
113 			
114 //			NodeInformationImpl temp = new NodeInformationImpl();
115 //			temp.setContextName("tttt");
116 	}
117 	public int checkNode(Long id, String contextName)
118 	{
119 		if ((contextName == null) || (id == null))
120 			return NodeManager.INVALID_NODE_REQUEST;
121 		NodeInformation info = (NodeInformation)nodes.get(contextName);
122 		if (info == null)
123 			return NodeManager.NODE_NEW;
124 		if (info.getId().longValue() < id.longValue())
125 			return NodeManager.NODE_OUTDATED;
126 		return NodeManager.NODE_SAVED;
127 	}
128 	
129 	public void addNode(Long id, String contextName) throws Exception
130 	{
131 		if ((contextName == null) || (id == null))
132 			return;
133 		NodeInformation info = (NodeInformation)nodes.get(contextName);
134 		if (info == null)
135 		{
136 			info = createNodeInformation();
137 			info.setContextName(contextName);
138 		}
139 		info.setId(id);
140 		nodes.put(contextName, info);
141 		save();
142 	}
143 
144 	public void removeNode(String contextName) throws Exception
145 	{
146 		if (contextName == null)
147 			return;
148 		NodeInformation info = (NodeInformation)nodes.get(contextName);
149 		if (info == null)
150 			return;
151 		nodes.remove(contextName);
152 		save();
153 	}
154 
155 	
156     /*
157      * (non-Javadoc)
158      * 
159      * @see org.apache.jetspeed.profiler.Profiler#createRuleCriterion()
160      */
161     protected NodeInformation createNodeInformation() throws ClassNotFoundException
162     {
163         try
164         {
165             NodeInformation nodeInformation = (NodeInformation) beanFactory.getBean(
166                     this.nodeInformationBean, NodeInformation.class);
167             return nodeInformation;
168         } catch (Exception e)
169         {
170             log.error("Failed to create nodeInformation for " + nodeInformationBean
171                     + " error : " + e.getLocalizedMessage());
172             throw new ClassNotFoundException("Spring failed to create the "
173                     + " nodeInformation bean.", e);
174         }
175 
176     }
177 
178     /*
179      * Method called automatically by Spring container upon initialization
180      * 
181      * @param beanFactory automatically provided by framework @throws
182      * BeansException
183      */
184     public void setBeanFactory(BeanFactory beanFactory) throws BeansException
185     {
186         this.beanFactory = beanFactory;
187     }
188 
189 	public int getNumberOfNodes()
190 	{
191 		return nodes.size();
192 	}
193 
194     
195     
196     
197 }