KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > rmi > IReportTCPServer


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * IReportTCPServer.java
28  *
29  * Created on August 17, 2006, 3:00 PM
30  *
31  */

32
33 package it.businesslogic.ireport.rmi;
34
35 import it.businesslogic.ireport.Report;
36 import it.businesslogic.ireport.gui.JReportFrame;
37 import it.businesslogic.ireport.gui.MainFrame;
38 import it.businesslogic.ireport.gui.WizardDialog;
39 import it.businesslogic.ireport.util.PageSize;
40 import java.io.DataInputStream JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.PrintStream JavaDoc;
43 import java.lang.reflect.InvocationTargetException JavaDoc;
44 import java.net.ServerSocket JavaDoc;
45 import java.net.Socket JavaDoc;
46 import javax.swing.SwingUtilities JavaDoc;
47
48 /**
49  *
50  * @author gtoffoli
51  */

52 public class IReportTCPServer implements Runnable JavaDoc {
53     
54   static IReportTCPServer mainInstance = null;
55   
56   public static IReportTCPServer getMainInstance()
57   {
58       if (mainInstance == null)
59       {
60           try {
61                 mainInstance = new IReportTCPServer();
62           } catch (Exception JavaDoc ex)
63           {
64               ex.printStackTrace();
65           }
66       }
67       
68       return mainInstance;
69   }
70   
71     /** Creates a new instance of IReportTCPServer */
72     public IReportTCPServer() {
73     }
74     
75     
76     public static void runServer()
77     {
78       if (MainFrame.getMainInstance().getProperties().getProperty( "enableRMIServer" ,"false").equals("true"))
79       {
80         Thread JavaDoc t = new Thread JavaDoc( IReportTCPServer.getMainInstance() );
81         t.start();
82         //SwingUtilities.invokeLater( IReportServerImpl.getMainInstance() );
83
}
84     }
85     
86     public void run() {
87
88         ServerSocket JavaDoc serverSocket = null;
89         DataInputStream JavaDoc is;
90         PrintStream JavaDoc os;
91
92         Socket JavaDoc clientSocket = null;
93
94         int port = 2100;
95         try {
96             port = Integer.parseInt( MainFrame.getMainInstance().getProperties().getProperty( "RMIServerPort" ,"2100"));
97         }
98         catch (Exception JavaDoc ex)
99         {
100             ex.printStackTrace();
101         }
102         
103         try {
104
105             serverSocket = new ServerSocket JavaDoc( port );
106             MainFrame.getMainInstance().logOnConsole("Demone listening on port: " + serverSocket.getLocalPort() );
107
108         } catch (IOException JavaDoc e) {
109
110                MainFrame.getMainInstance().logOnConsole("Error opening the socket : " + e.getMessage());
111            return;
112         }
113
114         // Non ci interessa gestire connessioni multiple...
115
while ( true )
116         {
117             is = null;
118             os = null;
119             clientSocket = null;
120             try {
121                 clientSocket = serverSocket.accept();
122                 is = new DataInputStream JavaDoc(clientSocket.getInputStream());
123
124                 final String JavaDoc line = is.readLine();
125                 os = new PrintStream JavaDoc(clientSocket.getOutputStream());
126
127                 if (line == null)
128                 {
129                     os.write( new String JavaDoc("-Unknow command!").getBytes() );
130                     os.close();
131                     clientSocket.close();
132                 } else {
133                     
134                      os.write( new String JavaDoc("+OK Give me five!").getBytes() );
135                      os.close();
136                      clientSocket.close();
137                      
138                     try {
139                         
140                         SwingUtilities.invokeAndWait( new Runnable JavaDoc()
141                         {
142                             public void run()
143                             {
144                         
145                         
146                     try {
147                            if (line.toUpperCase().startsWith("PING"))
148                            {
149                                
150                            }
151                            else if (line.toUpperCase().startsWith("WIZARD "))
152                            {
153                                runWizard(line.substring(7));
154                            }
155                            else if (line.toUpperCase().startsWith("OPENFILE "))
156                            {
157                                openFile(line.substring(9));
158                            }
159                            else if (line.toUpperCase().startsWith("SETVISIBLE "))
160                            {
161                                setVisible( Boolean.valueOf(line.substring(11).trim()).booleanValue() );
162                            }
163                            else
164                            {
165                                throw new Exception JavaDoc("Unknow command: " + line);
166                            }
167                            //os.write( new String("+OK Give me five!").getBytes() );
168

169                     } catch (Throwable JavaDoc tr)
170                     {
171                            tr.printStackTrace();
172                            //os.write( new String("-Error " + tr).getBytes() );
173
}
174                             }
175                     });
176                     } catch (InterruptedException JavaDoc ex) {
177                         ex.printStackTrace();
178                     } catch (InvocationTargetException JavaDoc ex) {
179                         ex.printStackTrace();
180                     }
181                     
182                 }
183             } catch (IOException JavaDoc e) {
184         }
185         }
186
187
188     }
189     
190     
191     // ---------------------------------------------------------------------------------
192

193      public boolean runWizard(String JavaDoc destFile)
194   {
195     MainFrame mainFrame = MainFrame.getMainInstance();
196     
197     if (mainFrame == null) return false;
198     
199         
200         
201     mainFrame.logOnConsole("Invocato wizard");
202     mainFrame.logOnConsole("Pronto ad invocare la nuova finestra..." + Thread.currentThread().getName());
203     
204         
205         try {
206                 // TODO
207
// Set the project directory as current directory;
208

209                 WizardDialog wd = new WizardDialog(mainFrame,true);
210
211                 mainFrame.logOnConsole("Lancio wizard");
212                 wd.setVisible(true);
213                 wd.requestFocus();
214
215
216                 Report report = null;
217                 if (wd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
218                     report = wd.getReport();
219                     if (report == null)
220                     {
221                         report = createBlankReport();
222                     }
223                 }
224                 else
225                 {
226                     report = createBlankReport();
227                 }
228
229                 if (report != null)
230                 {
231                         mainFrame.openNewReportWindow(report);
232                         report.setFilename(destFile);
233                         report.saveXMLFile();
234                         //setVisible(true);
235
}
236
237         } catch (Exception JavaDoc ex)
238         {
239               System.out.println(ex.getMessage());
240                 ex.printStackTrace();
241         }
242         
243
244       return true;
245   }
246   
247   private Report createBlankReport()
248   {
249       Report newReport = new Report();
250       
251         newReport.setName(it.businesslogic.ireport.util.I18n.getString("untitledReport", "untitled_report_")+"1");
252         newReport.setUsingMultiLineExpressions(false); //this.isUsingMultiLineExpressions());
253
newReport.setWidth( PageSize.A4.x);
254         newReport.setHeight( PageSize.A4.y);
255         newReport.setTopMargin(20);
256         newReport.setLeftMargin(30);
257         newReport.setRightMargin(30);
258         newReport.setBottomMargin(20);
259         newReport.setColumnCount(1);
260         newReport.setColumnWidth( newReport.getWidth() - newReport.getLeftMargin() - newReport.getRightMargin() );
261         newReport.setColumnSpacing(0);
262         
263         return newReport;
264   }
265   
266     /**
267    * Used to check if iReport is alive
268    */

269   public boolean ping()
270   {
271       return true;
272   }
273   
274   /**
275    * Used to show the main window and bring the iReport window on top...
276    */

277   public boolean setVisible(boolean b)
278   {
279       MainFrame.getMainInstance().setVisible(b);
280       if (MainFrame.getMainInstance().getState() == java.awt.Frame.ICONIFIED)
281       {
282             MainFrame.getMainInstance().setState( java.awt.Frame.NORMAL );
283       }
284       return MainFrame.getMainInstance().requestFocusInWindow();
285   }
286   
287   /**
288    * Open the file passed as parameter...
289    */

290   public boolean openFile(String JavaDoc file)
291   {
292       setVisible(true);
293       try {
294           JReportFrame jrf = MainFrame.getMainInstance().openFile( file );
295           jrf.setSelected(true);
296           return true;
297       } catch (Exception JavaDoc ex){
298            return false;
299       }
300   }
301 }
302
Popular Tags