KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > Deployment > ParameterConverter


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Briclet Frederic
23 Contributor(s):
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.Deployment;
28
29 public class ParameterConverter{
30  
31     /**
32      * All the different kind of parameter used
33      */

34     private ParameterType parameterTypes[] =
35                             new ParameterType[]{new ORBParameter(),
36                                                 new IORParameter(),
37                                                 new SimpleParameter()};
38     /**
39      * The definition of parameter type
40      */

41     private abstract class ParameterType {
42       
43      public abstract boolean is_a(String JavaDoc param);
44      
45      public abstract String JavaDoc convert(String JavaDoc param);
46      
47      public abstract boolean implyNextConvertion();
48     }
49
50     /**
51      * Basis parameter type
52      */

53     private class SimpleParameter extends ParameterType{
54  
55          public boolean is_a(String JavaDoc param)
56          {
57             return param!=null;
58          }
59          
60          public String JavaDoc convert(String JavaDoc param)
61          {
62             return param;
63          }
64       
65          public boolean
66         implyNextConvertion()
67         {
68             return false;
69         }
70         
71         public String JavaDoc
72         toString()
73         {
74             return "SimpleType";
75         }
76     }
77
78
79
80     class TestAuthenticator extends java.net.Authenticator JavaDoc {
81         
82            private String JavaDoc username = "titi";
83         private String JavaDoc password = "titi";
84         
85         public TestAuthenticator() {}
86         
87         public java.net.PasswordAuthentication JavaDoc getPasswordAuthentication()
88         {
89             return new java.net.PasswordAuthentication JavaDoc(username,(password.toCharArray()));
90             }
91     }
92
93     /**
94      * IOR parameter type containing containing only URL. Stringified
95      * IOR are considerer as simple parameter
96      */

97     private class IORParameter extends SimpleParameter {
98  
99          public boolean is_a(String JavaDoc param)
100          {
101             return param.endsWith(".IOR")||param.endsWith(".ior");
102          }
103          
104          public String JavaDoc convert(String JavaDoc param)
105          {
106
107            String JavaDoc ch=param.substring(param.indexOf("=")+1);
108                     try{
109                         
110                         //java.net.Authenticator.setDefault(new TestAuthenticator());
111
java.net.URL JavaDoc url=new java.net.URL JavaDoc(ch);
112                     
113                         //String userPassword = "anonymous:anonymous";
114

115                         // Encode String
116
//String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
117

118                          
119                         // Need to work with URLConnection to set request property
120
// java.net.URLConnection uc = url.openConnection();
121
//uc.setRequestProperty ("Authorization", "Basic " + encoding);
122
//System.err.println("Authorization fixed");
123

124                         java.io.InputStream JavaDoc in=url.openStream(); //uc.getInputStream();
125
byte []b=new byte[in.available()];
126                         
127                         in.read(b,0,in.available());
128                         ch=param.substring(0,param.indexOf("=")+1)+(new String JavaDoc(b)).trim();
129                         //System.out.println(args[i]);
130
}
131             catch(Exception JavaDoc e)
132                    {
133                     e.printStackTrace();
134                     System.err.println("Cannot read the URL contents.");
135                     return param ;
136                     
137                     }
138             return ch;
139          }
140          
141         public String JavaDoc
142         toString()
143         {
144             return "IORType";
145         }
146     }
147     
148     /**
149      * The ORB parameter can require convertion of the following parameter
150      * if it denote an IOR with an URL
151      */

152     public class ORBParameter extends SimpleParameter{
153       
154     
155          public boolean is_a(String JavaDoc param)
156          {
157             return param.startsWith("-ORB");
158          }
159          
160       
161         public boolean
162         implyNextConvertion()
163         {
164             return true;
165         }
166         
167         public String JavaDoc
168         toString()
169         {
170             return "ORBType";
171         }
172     }
173     
174     /**
175      * The basis parameter witch do all the treatment
176      */

177     private class Parameter{
178  
179      
180      private String JavaDoc arg=null;
181      private ParameterType type;
182      private Parameter next;
183      private int index;
184      
185      public Parameter(String JavaDoc args[],int index ){
186         this.arg=args[index];
187         this.index=index;
188         
189         for(int i=0;i<parameterTypes.length ;i++)
190             {
191                 if(parameterTypes[i].is_a(arg)){
192                     this.type=parameterTypes[i];
193                     break;
194                 }
195             }
196         if(args.length-1>index)
197             next= new Parameter(args,index+1);
198         
199      }
200  
201      public void convert(){
202         arg=type.convert(arg);
203     }
204      
205      public java.util.LinkedList JavaDoc
206      processTreatment()
207      {
208           
209         if(next==null){
210             java.util.LinkedList JavaDoc ll= new java.util.LinkedList JavaDoc();
211             ll.addFirst(arg);
212             return ll;
213           }
214           
215         if(type.implyNextConvertion())
216             next.convert();
217         
218         java.util.LinkedList JavaDoc ll=next.processTreatment();
219         ll.addFirst(arg);
220         return ll;
221      }
222
223      public ParameterType
224      getParameterType()
225         {
226             return type;
227         }
228            
229     }
230  
231     /**
232      * Static method to convert arguments
233      */

234     public String JavaDoc[]
235     convertParameter(String JavaDoc args[])
236     {
237        java.util.LinkedList JavaDoc ll=(new Parameter(args,0)).processTreatment();
238        
239        String JavaDoc [] ch=(String JavaDoc[])ll.toArray(new String JavaDoc[ll.size()]);
240              
241            
242            return ch;
243     }
244     
245     /*
246     public static void main(String args[]){
247         ParameterConverter pc=new ParameterConverter();
248         String []arg=pc.convertParameter(new String[]{"-ORBInitRef","0214541121","-ORBInitRef","TITI=http://127.0.0.1/dci_toto.IOR" });
249         for(int i=0;i<arg.length;i++)
250                System.err.println(arg[i]);
251     }*/

252            
253 }
254
Popular Tags