Coverage report

  %line %branch
org.apache.jetspeed.cluster.NodeManagerImpl
0% 
0% 

 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  0
 	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  0
 	private HashMap nodes = null; 
 48  0
 	private File rootIndexDir = null;
 49  
 	    
 50  
     /** the default criterion bean name */
 51  0
     private String nodeInformationBean = "NodeInformation";
 52  
 
 53  
 	
 54  
 	public NodeManagerImpl(String indexRoot, String nodeInformationBean)
 55  
     throws Exception
 56  0
     {
 57  
     	
 58  
         //assume it's full path for now
 59  0
         rootIndexDir = new File(indexRoot);
 60  0
         this.nodeInformationBean = nodeInformationBean;
 61  
 
 62  0
         if (!(rootIndexDir.exists()))
 63  0
         	rootIndexDir.mkdirs();
 64  0
         load();
 65  0
     }
 66  
 
 67  
 	
 68  
 	protected void save()
 69  
 	{
 70  
 		try {
 71  0
 		      FileOutputStream fout = new FileOutputStream(rootIndexDir.getAbsolutePath()+ "/nodeInfo.ser");
 72  0
 		      ObjectOutputStream oos = new ObjectOutputStream(fout);
 73  0
 		      oos.writeObject(nodes);
 74  0
 		      oos.close();
 75  
 		      }
 76  0
 		   catch (Exception e) 
 77  
 		   {
 78  0
 	            log.error("Failed to write nodes data file to  " + rootIndexDir.getAbsolutePath()+ "/nodeInfo.ser" + " - error : " + e.getLocalizedMessage());
 79  0
 			   e.printStackTrace(); 
 80  0
 			}
 81  0
 	}
 82  
     
 83  
 	protected void load()
 84  
 	{
 85  0
 			File data = new File( rootIndexDir.getAbsolutePath()+ "/nodeInfo.ser");
 86  0
 			if (data.exists())
 87  
 			{
 88  
 				try {
 89  0
 				    FileInputStream fin = new FileInputStream(data.getAbsolutePath());
 90  0
 				    ObjectInputStream ois = new ObjectInputStream(fin);
 91  0
 				    nodes = (HashMap) ois.readObject();
 92  0
 				    ois.close();
 93  
 				    }
 94  0
 				   catch (Exception e) 
 95  
 				   { 
 96  0
 			            log.error("Failed to read nodes data file from " + data.getAbsolutePath() + " - error : " + e.getLocalizedMessage());
 97  0
 					   nodes = new HashMap();
 98  0
 				   }
 99  
 			}
 100  
 			else
 101  
 			{
 102  
 				try
 103  
 				{
 104  0
 					data.createNewFile();
 105  
 				}
 106  0
 				catch (Exception e)
 107  
 				{
 108  0
 		            log.error("Failed to create new nodes data file error : " + e.getLocalizedMessage());
 109  0
 					e.printStackTrace();
 110  0
 				}
 111  0
 				nodes = new HashMap();
 112  
 			}
 113  
 			
 114  
 //			NodeInformationImpl temp = new NodeInformationImpl();
 115  
 //			temp.setContextName("tttt");
 116  0
 	}
 117  
 	public int checkNode(Long id, String contextName)
 118  
 	{
 119  0
 		if ((contextName == null) || (id == class="keyword">null))
 120  0
 			return NodeManager.INVALID_NODE_REQUEST;
 121  0
 		NodeInformation info = (NodeInformation)nodes.get(contextName);
 122  0
 		if (info == null)
 123  0
 			return NodeManager.NODE_NEW;
 124  0
 		if (info.getId().longValue() < id.longValue())
 125  0
 			return NodeManager.NODE_OUTDATED;
 126  0
 		return NodeManager.NODE_SAVED;
 127  
 	}
 128  
 	
 129  
 	public void addNode(Long id, String contextName) throws Exception
 130  
 	{
 131  0
 		if ((contextName == null) || (id == class="keyword">null))
 132  0
 			return;
 133  0
 		NodeInformation info = (NodeInformation)nodes.get(contextName);
 134  0
 		if (info == null)
 135  
 		{
 136  0
 			info = createNodeInformation();
 137  0
 			info.setContextName(contextName);
 138  
 		}
 139  0
 		info.setId(id);
 140  0
 		nodes.put(contextName, info);
 141  0
 		save();
 142  0
 	}
 143  
 
 144  
 	public void removeNode(String contextName) throws Exception
 145  
 	{
 146  0
 		if (contextName == null)
 147  0
 			return;
 148  0
 		NodeInformation info = (NodeInformation)nodes.get(contextName);
 149  0
 		if (info == null)
 150  0
 			return;
 151  0
 		nodes.remove(contextName);
 152  0
 		save();
 153  0
 	}
 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  0
             NodeInformation nodeInformation = (NodeInformation) beanFactory.getBean(
 166  
                     this.nodeInformationBean, NodeInformation.class);
 167  0
             return nodeInformation;
 168  0
         } catch (Exception e)
 169  
         {
 170  0
             log.error("Failed to create nodeInformation for " + nodeInformationBean
 171  
                     + " error : " + e.getLocalizedMessage());
 172  0
             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  0
         this.beanFactory = beanFactory;
 187  0
     }
 188  
 
 189  
 	public int getNumberOfNodes()
 190  
 	{
 191  0
 		return nodes.size();
 192  
 	}
 193  
 
 194  
     
 195  
     
 196  
     
 197  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.