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.IOException;
20  import java.io.ObjectInputStream;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  import java.text.DateFormat;
24  import java.util.Date;
25  
26  import org.apache.pluto.om.common.ObjectID;
27  
28  /***
29   * Node Information
30   * 
31   * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
32   * @version
33   */
34  public class NodeInformationImpl implements NodeInformation, Serializable 
35  {
36  	static final long serialVersionUID = -598265530537353219L;
37  
38  	private Long id;
39  	private String contextName;
40  	private Date lastDeployDate = null;
41  	private static final int CompressVersion = 1;
42  
43  	/***
44  	 * default class construtor required for bean management
45  	 *
46  	 */
47  	public NodeInformationImpl()
48  	{}
49  	
50  	/***
51  	 * extensible serialization routine - indicates the version written to allow for later structural updates
52  	 *
53  	 */
54  	private void writeObject(ObjectOutputStream out) throws IOException
55  	{
56  		out.writeByte(CompressVersion);
57  		out.writeLong(id.longValue());
58  		out.writeUTF(contextName);
59  		if (lastDeployDate == null)
60  			out.writeByte(0);
61  		else
62  		{
63  			out.writeByte(1);
64  			out.writeLong(lastDeployDate.getTime());
65  		}
66  	}
67  	/***
68  	 * extensible serialization routine 
69  	 * using the version byte code can identify older versions and handle updates correctly
70  	 *
71  	 */
72  	private void readObject(ObjectInputStream in) throws IOException,
73  			ClassNotFoundException
74  	{
75  		int version = in.readByte();
76  		// do changes here if version dependant
77  
78  		id = new Long(in.readLong());
79  		contextName = in.readUTF();
80  		int dateSet = in.readByte();
81  		
82  		if (dateSet == 1)
83  			lastDeployDate = new Date(in.readLong());
84  		else
85  			lastDeployDate = null;
86  	}
87  
88  	public boolean equals(Object object)
89  	{
90  		if (object == this)
91  			return true;
92  		if (!(object instanceof NodeInformation))
93  			return false;
94  		return equals((NodeInformation) object);
95  	}
96  
97  	public int compareTo(Object object)
98  	{
99  		if (object == null)
100 			return 1;
101 		if (object == this)
102 			return 0;
103 		if (!(object instanceof NodeInformation))
104 			return 1;
105 		return compareTo((NodeInformation) object);
106 	}
107 
108 	public final boolean equals(NodeInformation object)
109 	{
110 		if (object == null)
111 			return false;
112 		return object.getContextName().equalsIgnoreCase(contextName);
113 	}
114 
115 	public final int compareTo(NodeInformation object)
116 	{
117 		return getContextName().compareToIgnoreCase(contextName);
118 	}
119 
120 	public String toString()
121 	{
122 		StringBuffer buffer = new StringBuffer();
123 		buffer.append("id= " + this.id.longValue());
124 		buffer.append("; contextName= " + this.getContextName());
125 		buffer.append("; lastDeployDate= " + this.getContextName());
126 		if (this.lastDeployDate != null)
127 		{
128 			DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
129 			try
130 			{
131 				buffer.append(format.format(this.lastDeployDate));
132 			} catch (Exception e)
133 			{
134 				buffer.append("<invalidDate>");
135 			}
136 		} else
137 			buffer.append("<empty>");
138 
139 		return buffer.toString();
140 	}
141 
142 	public String getContextName()
143 	{
144 		return contextName;
145 	}
146 
147 	public void setContextName(String contextName)
148 	{
149 		this.contextName = contextName;
150 	}
151 
152 	public Long getId()
153 	{
154 		return id;
155 	}
156 
157 	public void setId(ObjectID id)
158 	{
159 		this.id = new Long(id.toString());
160 	}
161 
162 	public void setId(Long id)
163 	{
164 		this.id = id;
165 	}
166 
167 	public void setId(long id)
168 	{
169 		this.id = new Long(id);
170 	}
171 
172 	public Date getLastDeployDate()
173 	{
174 		return lastDeployDate;
175 	}
176 
177 	public void setLastDeployDate(Date lastDeployDate)
178 	{
179 		this.lastDeployDate = lastDeployDate;
180 	}
181 
182 }