KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > filetransfer > cif > ProgImpl


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

27
28 package org.objectweb.ccm.filetransfer.cif;
29
30 //import org.objectweb.ccm.filetransfer.*;
31

32 /**
33  * This is the implementation of the OMG IDL3 filetransfer::Prog component type.
34  *
35  * This class inherits from the local CCM_Prog interface
36  * generated by the OpenCCM's IDL3 to IDL2 mapping generator.
37  *
38  *
39  * @author <a HREF="mailto:Areski.Flissi@lifl.fr">Areski Flissi</A>
40  */

41
42 public class ProgImpl
43     extends org.objectweb.ccm.filetransfer.ProgSessionComposition.ComponentImpl
44     implements java.lang.Runnable JavaDoc, java.awt.event.ActionListener JavaDoc {
45     // ==================================================================
46
//
47
// Internal state.
48
//
49
// ==================================================================
50

51     /** The name of the component. */
52     private String JavaDoc name_;
53
54     private String JavaDoc prog_dir;
55     private String JavaDoc[] fileList = null;
56     
57
58     private Thread JavaDoc the_prog;
59
60     /** To refer to the GUI frame. */
61     private java.awt.Frame JavaDoc frame_;
62     private java.awt.Button JavaDoc button_start;
63
64     /** To refer to the GUI user output area. */
65     private java.awt.TextArea JavaDoc textArea_;
66
67     // ==================================================================
68
//
69
// Constructor.
70
//
71
// ==================================================================
72

73     /** The default constructor. */
74     public ProgImpl() {
75
76         
77     }
78
79     // ==================================================================
80
//
81
// Internal methods.
82
//
83
// ==================================================================
84

85     // ==================================================================
86
//
87
// Public methods.
88
//
89
// ==================================================================
90

91     // ==================================================================
92
//
93
// Methods for OMG IDL Components::EnterpriseComponent
94
//
95
// ==================================================================
96

97     /**
98      * Complete the component configuration.
99      *
100      * @exception org.omg.Components.InvalidConfiguration
101      * Thrown if the configuration is invalid.
102      */

103     public void configuration_complete()
104         throws org.omg.Components.InvalidConfiguration {
105
106         // Checks if the configuration is not completed.
107
if (name_ == null)
108             throw new org.omg.Components.InvalidConfiguration();
109
110 // // Instantiating the GUI.
111
// // Creates a AWT Frame.
112
// frame_ = new java.awt.Frame("The " + name_ + " Prog's GUI");
113
//
114
// // Creates a text area for displaying inputs.
115
// textArea_ = new java.awt.TextArea(12, 40);
116
// textArea_.setEditable(false);
117
//
118
// //button_start = new java.awt.Button("Start");
119
// //button_start.addActionListener(this);
120
//
121
// // Constructs and shows the GUI.
122
// java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
123
// frame_.setLayout(new java.awt.BorderLayout());
124
// frame_.add(panel);
125
//
126
// panel.add(textArea_, java.awt.BorderLayout.NORTH);
127
// //panel.add(button_start, java.awt.BorderLayout.CENTER);
128
//
129
// frame_.pack();
130
// frame_.show();
131

132         // Start a new thread to send files.
133
the_prog = new java.lang.Thread JavaDoc(this);
134         the_prog.start();
135
136         
137     }
138
139     // ==================================================================
140
//
141
// Methods for the OMG IDL org.omg.Components.SessionComponent
142
//
143
// ==================================================================
144

145     /**
146      * Container callback to signal that the component is removed.
147      *
148      * @throw org.omg.Components.CCMException For any problems.
149      */

150     public void ccm_remove() throws org.omg.Components.CCMException {
151
152     }
153
154     // ==================================================================
155
//
156
// Methods for the OMG IDL filetransfer::CCM_Prog interface.
157
//
158
// ==================================================================
159

160     /**
161          * The mutator method for the attribute name.
162          *
163          * @param n The name.
164          */

165     public void the_name(String JavaDoc n) {
166         name_ = n;
167
168     }
169
170     /**
171      * The accessor method for the attribute name.
172      *
173      * @return The name.
174      */

175     public String JavaDoc the_name() {
176         return name_;
177     }
178
179     // ==================================================================
180
//
181
// Methods for sending files to connected channels.
182
//
183
// ==================================================================
184

185     public void send_files() {
186
187         // Getting the Prog directory
188

189               
190         java.io.File JavaDoc currentdir = new java.io.File JavaDoc(System.getProperty("user.dir"));
191         currentdir = new java.io.File JavaDoc(currentdir.getParent());
192         java.io.File JavaDoc path = new java.io.File JavaDoc(currentdir.getParent() + "/IMAGE/Send/" + name_);
193         System.out.println(path);
194         
195         
196         fileList = path.list();
197
198         // Sending all files
199
for (int i = 0; i < fileList.length; i++) {
200             
201             
202             
203             java.io.File JavaDoc testfile = new java.io.File JavaDoc(path + "/" + fileList[i]);
204             if (testfile.isFile()) {
205             
206             //
207
// Read data from the file to be transferred
208
//
209
java.io.FileInputStream JavaDoc inStream = null;
210
211             try {
212                 
213                 
214                 System.out.println(
215                     "Sending "
216                         + path
217                         + "\\"
218                         + fileList[i]
219                         + " from "
220                         + name_
221                         + " to Channel component");
222                 inStream =
223                     new java.io.FileInputStream JavaDoc(path + "/" + fileList[i]);
224             } catch (java.io.FileNotFoundException JavaDoc ex) {
225                 System.err.println(
226                     "Can't open file " + fileList[i] + ex.getMessage());
227
228             }
229
230             byte[] file = null;
231             try {
232                 file = new byte[inStream.available()];
233                 inStream.read(file);
234                 inStream.close();
235             } catch (java.io.IOException JavaDoc ex) {
236                 System.err.println(
237                     "Can't read from " + fileList[i] + ex.getMessage());
238
239             }
240
241             //
242
// Send the file
243
//
244

245             get_context().push_to_channels(new ImagesImpl(file, fileList[i]));
246
247             //textArea_.append("Sending " + fileList[i] + "..." + "\n");
248

249             // Gros dodo pendant x secondes
250
try {
251                 Thread.sleep(2000);
252             } catch (InterruptedException JavaDoc ex) {
253                 // None
254
}
255             }
256         }
257
258     }
259
260     public void run() {
261
262         for (int j = 0; j < 100; j++) {
263             this.send_files();
264
265         }
266
267     }
268
269     // ==================================================================
270
//
271
// Methods for interface java.awt.event.ActionListener
272
//
273
// ==================================================================
274

275     /**
276      * When the button is selectionned.
277      *
278      * @param e The associate ActionEvent.
279      */

280     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
281
282         // Start a new thread to send files.
283
the_prog = new java.lang.Thread JavaDoc(this);
284         the_prog.start();
285
286     }
287
288 }
289
Popular Tags