portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tay...@apache.org
Subject svn commit: r1610047 - /portals/jetspeed-2/portal/branches/JETSPEED-BRANCH-2.2.2-POST-RELEASE/components/jetspeed-portal/src/main/java/org/apache/jetspeed/cluster/NodeManagerImpl.java
Date Sat, 12 Jul 2014 20:51:41 GMT
Author: taylor
Date: Sat Jul 12 20:51:41 2014
New Revision: 1610047

URL: http://svn.apache.org/r1610047
Log:
JS2-1285: backporting changes from trunk to 2.2.3 maintenance release

Modified:
    portals/jetspeed-2/portal/branches/JETSPEED-BRANCH-2.2.2-POST-RELEASE/components/jetspeed-portal/src/main/java/org/apache/jetspeed/cluster/NodeManagerImpl.java

Modified: portals/jetspeed-2/portal/branches/JETSPEED-BRANCH-2.2.2-POST-RELEASE/components/jetspeed-portal/src/main/java/org/apache/jetspeed/cluster/NodeManagerImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/JETSPEED-BRANCH-2.2.2-POST-RELEASE/components/jetspeed-portal/src/main/java/org/apache/jetspeed/cluster/NodeManagerImpl.java?rev=1610047&r1=1610046&r2=1610047&view=diff
==============================================================================
--- portals/jetspeed-2/portal/branches/JETSPEED-BRANCH-2.2.2-POST-RELEASE/components/jetspeed-portal/src/main/java/org/apache/jetspeed/cluster/NodeManagerImpl.java
(original)
+++ portals/jetspeed-2/portal/branches/JETSPEED-BRANCH-2.2.2-POST-RELEASE/components/jetspeed-portal/src/main/java/org/apache/jetspeed/cluster/NodeManagerImpl.java
Sat Jul 12 20:51:41 2014
@@ -5,9 +5,9 @@
  * The ASF licenses this file to You under the Apache License, Version 2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,146 +16,205 @@
  */
 package org.apache.jetspeed.cluster;
 
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.util.HashMap;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.BeanFactory;
-import org.springframework.beans.factory.BeanFactoryAware;
+import java.util.Map;
 
 /**
- * Node Manager 
+ * Node Manager
  *
  * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
- * @version 
+ * @version $Id$
  */
 public class NodeManagerImpl implements NodeManager,BeanFactoryAware
 {
-	protected final static Logger log = LoggerFactory.getLogger(NodeManagerImpl.class);
+
+    protected final static Logger log = LoggerFactory.getLogger(NodeManagerImpl.class);
+
+    private static final String NODES_SERIALIZED_FILE_NAME = "nodeInfo.ser";
 
     /**
      * added support for bean factory to create profile rules
      */
     private BeanFactory beanFactory;
 
-	private HashMap<String, NodeInformation> nodes = null; 
-	private File rootIndexDir = null;
-	    
+    private Map<String, NodeInformation> nodes = new HashMap<String, NodeInformation>();
+    private File rootIndexDir = null;
+
     /** the default criterion bean name */
     private String nodeInformationBean = "NodeInformation";
 
-	
-	public NodeManagerImpl(String indexRoot, String nodeInformationBean)
-    throws Exception
+
+    public NodeManagerImpl(String indexRoot, String nodeInformationBean) throws Exception
     {
-    	
         //assume it's full path for now
         rootIndexDir = new File(indexRoot);
         this.nodeInformationBean = nodeInformationBean;
 
-        if (!(rootIndexDir.exists()))
-        	rootIndexDir.mkdirs();
+        if (!rootIndexDir.isDirectory())
+        {
+            rootIndexDir.mkdirs();
+        }
+
         load();
     }
 
-	
-	protected void save()
-	{
-		try {
-		      FileOutputStream fout = new FileOutputStream(rootIndexDir.getAbsolutePath()+ "/nodeInfo.ser");
-		      ObjectOutputStream oos = new ObjectOutputStream(fout);
-		      oos.writeObject(nodes);
-		      oos.close();
-		      }
-		   catch (Exception e) 
-		   {
-	            log.error("Failed to write nodes data file to  " + rootIndexDir.getAbsolutePath()+
"/nodeInfo.ser" + " - error : " + e.getLocalizedMessage());
-			   e.printStackTrace(); 
-			}
-	}
-    
-	@SuppressWarnings("unchecked")
+
+    protected void save()
+    {
+        File nodesFile = new File(rootIndexDir, NODES_SERIALIZED_FILE_NAME);
+
+        FileOutputStream fos = null;
+        BufferedOutputStream bos = null;
+        ObjectOutputStream oos = null;
+
+        try
+        {
+            fos = new FileOutputStream(nodesFile);
+            bos = new BufferedOutputStream(fos);
+            oos = new ObjectOutputStream(bos);
+
+            oos.writeObject(nodes);
+        }
+        catch (Exception e)
+        {
+            log.error("Failed to write nodes data to " + nodesFile, e);
+        }
+        finally
+        {
+            IOUtils.closeQuietly(oos);
+            IOUtils.closeQuietly(bos);
+            IOUtils.closeQuietly(fos);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
     protected void load()
-	{
-			File data = new File( rootIndexDir.getAbsolutePath()+ "/nodeInfo.ser");
-			if (data.exists())
-			{
-				try {
-				    FileInputStream fin = new FileInputStream(data.getAbsolutePath());
-				    ObjectInputStream ois = new ObjectInputStream(fin);
-				    nodes = (HashMap<String,NodeInformation>) ois.readObject();
-				    ois.close();
-				    }
-				   catch (Exception e) 
-				   { 
-			            log.error("Failed to read nodes data file from " + data.getAbsolutePath()
+ " - error : " + e.getLocalizedMessage());
-					   nodes = new HashMap<String,NodeInformation>();
-				   }
-			}
-			else
-			{
-				try
-				{
-					data.createNewFile();
-				}
-				catch (Exception e)
-				{
-		            log.error("Failed to create new nodes data file error : " + e.getLocalizedMessage());
-					e.printStackTrace();
-				}
-				nodes = new HashMap<String,NodeInformation>();
-			}
-			
-//			NodeInformationImpl temp = new NodeInformationImpl();
-//			temp.setContextName("tttt");
-	}
-	public synchronized int checkNode(Long revision, String contextName)
-	{
-		if ((contextName == null) || (revision == null))
-			return NodeManager.INVALID_NODE_REQUEST;
-		NodeInformation info = (NodeInformation)nodes.get(contextName);
-		if (info == null)
-			return NodeManager.NODE_NEW;
-		if (info.getRevision().longValue() < revision.longValue())
-			return NodeManager.NODE_OUTDATED;
-		return NodeManager.NODE_SAVED;
-	}
-	
-	public synchronized void addNode(Long revision, String contextName) throws Exception
-	{
-		if ((contextName == null) || (revision == null))
-			return;
-		NodeInformation info = (NodeInformation)nodes.get(contextName);
-		if (info == null)
-		{
-			info = createNodeInformation();
-			info.setContextName(contextName);
-		}
-		info.setRevision(revision);
-		nodes.put(contextName, info);
-		save();
-	}
-
-	public synchronized void removeNode(String contextName) throws Exception
-	{
-		if (contextName == null)
-			return;
-		NodeInformation info = (NodeInformation)nodes.get(contextName);
-		if (info == null)
-			return;
-		nodes.remove(contextName);
-		save();
-	}
-	
+    {
+        File nodesFile = new File(rootIndexDir, NODES_SERIALIZED_FILE_NAME);
+
+        FileInputStream fis = null;
+        BufferedInputStream bis = null;
+        ObjectInputStream ois = null;
+
+        if (nodesFile.isFile())
+        {
+            // In order to avoid EOFException, check whether or not the file is empty before
reading.
+            if (nodesFile.length() > 0) {
+                try
+                {
+                    fis = new FileInputStream(nodesFile);
+                    bis = new BufferedInputStream(fis);
+                    ois = new ObjectInputStream(bis);
+
+                    nodes = (HashMap<String, NodeInformation>) ois.readObject();
+                }
+                catch (Exception e)
+                {
+                    log.error("Failed to read nodes data from " + nodesFile, e);
+                    nodes = new HashMap<String, NodeInformation>();
+                }
+                finally
+                {
+                    IOUtils.closeQuietly(ois);
+                    IOUtils.closeQuietly(bis);
+                    IOUtils.closeQuietly(fis);
+                }
+            } else {
+                nodes = new HashMap<String, NodeInformation>();
+            }
+        }
+        else
+        {
+            try
+            {
+                nodesFile.createNewFile();
+            }
+            catch (Exception e)
+            {
+                log.error("Failed to create new nodes data file.", e);
+            }
+
+            nodes = new HashMap<String, NodeInformation>();
+        }
+    }
+
+    public synchronized int checkNode(Long revision, String contextName)
+    {
+        if ((contextName == null) || (revision == null))
+        {
+            return NodeManager.INVALID_NODE_REQUEST;
+        }
+
+        NodeInformation info = (NodeInformation) nodes.get(contextName);
+
+        if (info == null)
+        {
+            return NodeManager.NODE_NEW;
+        }
+
+        if (info.getRevision().longValue() < revision.longValue())
+        {
+            return NodeManager.NODE_OUTDATED;
+        }
+
+        return NodeManager.NODE_SAVED;
+    }
+
+    public synchronized void addNode(Long revision, String contextName) throws Exception
+    {
+        if ((contextName == null) || (revision == null))
+        {
+            return;
+        }
+
+        NodeInformation info = (NodeInformation) nodes.get(contextName);
+
+        if (info == null)
+        {
+            info = createNodeInformation();
+            info.setContextName(contextName);
+        }
+
+        info.setRevision(revision);
+        nodes.put(contextName, info);
+        save();
+    }
+
+    public synchronized void removeNode(String contextName) throws Exception
+    {
+        if (contextName == null)
+        {
+            return;
+        }
+
+        NodeInformation info = (NodeInformation) nodes.get(contextName);
+
+        if (info == null)
+        {
+            return;
+        }
+
+        nodes.remove(contextName);
+        save();
+    }
+
     /*
      * (non-Javadoc)
-     * 
+     *
      * @see org.apache.jetspeed.profiler.Profiler#createRuleCriterion()
      */
     protected NodeInformation createNodeInformation() throws ClassNotFoundException
@@ -165,7 +224,8 @@ public class NodeManagerImpl implements 
             NodeInformation nodeInformation = (NodeInformation) beanFactory.getBean(
                     this.nodeInformationBean, NodeInformation.class);
             return nodeInformation;
-        } catch (Exception e)
+        }
+        catch (Exception e)
         {
             log.error("Failed to create nodeInformation for " + nodeInformationBean
                     + " error : " + e.getLocalizedMessage());
@@ -177,7 +237,7 @@ public class NodeManagerImpl implements 
 
     /*
      * Method called automatically by Spring container upon initialization
-     * 
+     *
      * @param beanFactory automatically provided by framework @throws
      * BeansException
      */
@@ -186,8 +246,8 @@ public class NodeManagerImpl implements 
         this.beanFactory = beanFactory;
     }
 
-	public synchronized int getNumberOfNodes()
-	{
-		return nodes.size();
-	}    
+    public synchronized int getNumberOfNodes()
+    {
+        return nodes.size();
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message