1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: GenericReferenceable.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   //
27   package com.mjh.switchrmi;
28   
29   import java.util.*;
30   
31   import javax.naming.*;
32   
33   import org.apache.log4j.*;
34   
35   public class GenericReferenceable
36       implements Referenceable
37   {
38       private static final Logger log = 
39               Logger.getLogger(GenericReferenceable.class.getName());
40       private String className = "java.lang.Object";
41       private String codebase;
42       private List libs = new ArrayList();
43       private String description;
44       private String factoryClassName = 
45               "com.mjh.switchrmi.jndi.ObjectFactoryImpl";
46   
47       public GenericReferenceable()
48       {
49       }
50   
51       public GenericReferenceable(String name)
52       {
53           className = name;
54       }
55   
56       public String toString()
57       {
58           StringBuffer sb = new StringBuffer();
59   
60           sb.append("<GenericReferenceable\n");
61           appendMembers(sb);
62           sb.append("</GenericReferenceable>");
63   
64           return sb.toString();
65       }
66   
67       protected void appendMembers(StringBuffer sb)
68       {
69           sb.append("  <className>" + className + "</className>");
70           sb.append("  <codebase>" + codebase + "</codebase>");
71           sb.append("  <description>" + description + "</description>");
72           sb.append("  <factoryClassName>" + factoryClassName
73                     + "</factoryClassName>");
74           sb.append("  <libs>" + libs + "</libs>");
75       }
76   
77       public void setClassName(String value)
78       {
79           className = value;
80       }
81   
82       public String getClassName()
83       {
84           return className;
85       }
86   
87       public void setCodebase(String value)
88       {
89           codebase = value;
90       }
91   
92       public String getCodebase()
93       {
94           return codebase;
95       }
96   
97       public void addLibrary(String value)
98       {
99           libs.add(value);
100      }
101  
102      public List getLibraries()
103      {
104          return libs;
105      }
106  
107      public void setDescription(String value)
108      {
109          description = value;
110      }
111  
112      public String getDescription()
113      {
114          return description;
115      }
116  
117      public void setFactoryClassName(String value)
118      {
119          factoryClassName = value;
120      }
121  
122      public String getFactoryClassName()
123      {
124          return factoryClassName;
125      }
126  
127      public Reference getReference()
128                             throws NamingException
129      {
130          return new Reference(className, factoryClassName, 
131                               buildClasspath(codebase, libs));
132      }
133  
134      private String buildClasspath(String baseUrl, List libs)
135      {
136          String path = null;
137  
138          if (baseUrl != null)
139          {
140              if (!baseUrl.endsWith("/"))
141              {
142                  baseUrl += "/";
143              }
144  
145              StringBuffer sb = new StringBuffer();
146  
147              sb.append(baseUrl);
148  
149              if (libs.size() > 0)
150              {
151                  sb.append(" ");
152              }
153  
154              Iterator iter = libs.iterator();
155  
156              while (iter.hasNext())
157              {
158                  String lib = (String) iter.next();
159  
160                  while (lib.startsWith("/"))
161                  {
162                      lib = lib.substring(1);
163                  }
164  
165                  sb.append(baseUrl);
166                  sb.append(lib);
167  
168                  if (iter.hasNext())
169                  {
170                      sb.append(" ");
171                  }
172              }
173  
174              path = sb.toString();
175          }
176  
177          if (log.isDebugEnabled())
178          {
179              log.debug("path = " + path);
180          }
181  
182          return path;
183      }
184  }