View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18   
19  package jgroup.relacs.config;
20  
21  import java.io.Serializable;
22  import java.util.Map;
23  
24  /**
25   *  ClassData specifies the class and arguments of a replica.
26   *
27   *  @author Hein Meling
28   *  @since Jgroup 1.2
29   */
30  public class ClassData 
31    implements Serializable
32  {
33  
34    private static final long serialVersionUID = -504244408760314856L;
35  
36    /////////////////////////////////////////////////////////////////////////////
37    // Fields
38    /////////////////////////////////////////////////////////////////////////////
39  
40    /** Fully qualified class name */
41    private String className;
42  
43    /** Arguments passed to the main method of the above class */
44    private String[] classArgs;
45  
46    /**
47     * The map of system properties (key and value) 
48     * to append to the JVM command line
49     */
50    private Map<String,String> properties = null;
51  
52    /** Class name without package information */
53    private transient String shortClassName;
54  
55  
56    /////////////////////////////////////////////////////////////////////////////
57    // Constructors
58    /////////////////////////////////////////////////////////////////////////////
59  
60    public ClassData(String className, String[] classArgs)
61  //    throws ClassNotFoundException
62    {
63  //    Class cl = Class.forName(className);
64      this.className = className;
65      this.classArgs = classArgs;
66    }
67  
68  
69    /////////////////////////////////////////////////////////////////////////////
70    // Access methods
71    /////////////////////////////////////////////////////////////////////////////
72  
73    public Class getClassObject()
74    {
75      Class cl = null;
76      try {
77        cl = Class.forName(className);
78      } catch (ClassNotFoundException cnf) {
79        /*
80         * Should never happen since we throw an exception in the
81         * constructor if the class was not found, and thus we cannot get
82         * this far if the class was not available.
83         */
84      }
85      return cl;
86    }
87  
88    public String getName()
89    {
90      return className;
91    }
92  
93    public String getShortName()
94    {
95      if (shortClassName == null) {
96        shortClassName = className.substring(className.lastIndexOf('.') + 1);
97      }
98      return shortClassName;
99    }
100 
101   public String[] getArgs()
102   {
103     return classArgs;
104   }
105 
106   public String getArgString()
107   {
108     StringBuilder buf = new StringBuilder();
109     for (int i = 0; i < classArgs.length; i++) {
110       buf.append((i != 0) ? " " : "");
111       buf.append(classArgs[i]);
112     }
113     return buf.toString();
114   }
115 
116   public void setProperties(Map<String,String> properties)
117   {
118     this.properties = properties;
119   }
120 
121   public String getCommand(String jvmCmd, String appStarter)
122   {
123     StringBuilder buf = new StringBuilder(jvmCmd);
124     buf.append(" -Djgroup.log.app=");
125     buf.append(getShortName());
126     if (properties != null) {
127       for (Map.Entry<String,String> entry : properties.entrySet()) {
128         buf.append(" -D");
129         buf.append(entry.getKey());
130         buf.append("=");
131         buf.append(entry.getValue());
132       }
133     }
134     buf.append(" ");
135     buf.append(appStarter);
136     buf.append(" ");
137     buf.append(className);
138     buf.append(" ");
139     buf.append(getArgString());
140     return buf.toString();
141   }
142 
143   /////////////////////////////////////////////////////////////////////////////
144   // Object methods; overriding from the Object class
145   /////////////////////////////////////////////////////////////////////////////
146 
147   /**
148    *  Returns a string representation of the object.
149    */
150   public String toString()
151   {
152     StringBuilder buf = new StringBuilder();
153     buf.append("[");
154     buf.append(className);
155     if (classArgs.length > 0) {
156       buf.append(", ");
157       buf.append(getArgString());
158     }
159     buf.append("]");
160     return buf.toString();
161   }
162 
163   /**
164    *  Returns a hashcode for the <code>ClassData</code> class.
165    *  Two <code>ClassData</code> objects will return the same hashcode
166    *  if their class name and arguments are equal.
167    */
168   public int hashCode()
169   {
170     int hash = className.hashCode();
171     for (int i = 0; i < classArgs.length; i++)
172       hash ^= classArgs[i].hashCode();
173     return hash;
174   }
175 
176   /**
177    * Compares two objects for content equality.
178    *
179    * @param obj the object to compare with
180    * @return  true if these objects are equal; false otherwise.
181    */
182   public boolean equals(Object obj)
183   {
184     if (!(obj instanceof ClassData))
185       return false;
186     ClassData cd = (ClassData) obj;
187     if (!(className.equals(cd.className)) || classArgs.length != cd.classArgs.length) {
188       return false;
189     }
190     boolean eq = true;
191     for (int i = 0; eq && i < classArgs.length; i++) {
192       eq &= classArgs.equals(cd.classArgs);
193     }
194     return eq;
195   }
196 
197 } // END of ClassData