1 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 ; 41 import java.io.IOException ; 42 import java.io.PrintStream ; 43 import java.lang.reflect.InvocationTargetException ; 44 import java.net.ServerSocket ; 45 import java.net.Socket ; 46 import javax.swing.SwingUtilities ; 47 48 52 public class IReportTCPServer implements Runnable { 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 ex) 63 { 64 ex.printStackTrace(); 65 } 66 } 67 68 return mainInstance; 69 } 70 71 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 t = new Thread ( IReportTCPServer.getMainInstance() ); 81 t.start(); 82 } 84 } 85 86 public void run() { 87 88 ServerSocket serverSocket = null; 89 DataInputStream is; 90 PrintStream os; 91 92 Socket clientSocket = null; 93 94 int port = 2100; 95 try { 96 port = Integer.parseInt( MainFrame.getMainInstance().getProperties().getProperty( "RMIServerPort" ,"2100")); 97 } 98 catch (Exception ex) 99 { 100 ex.printStackTrace(); 101 } 102 103 try { 104 105 serverSocket = new ServerSocket ( port ); 106 MainFrame.getMainInstance().logOnConsole("Demone listening on port: " + serverSocket.getLocalPort() ); 107 108 } catch (IOException e) { 109 110 MainFrame.getMainInstance().logOnConsole("Error opening the socket : " + e.getMessage()); 111 return; 112 } 113 114 while ( true ) 116 { 117 is = null; 118 os = null; 119 clientSocket = null; 120 try { 121 clientSocket = serverSocket.accept(); 122 is = new DataInputStream (clientSocket.getInputStream()); 123 124 final String line = is.readLine(); 125 os = new PrintStream (clientSocket.getOutputStream()); 126 127 if (line == null) 128 { 129 os.write( new String ("-Unknow command!").getBytes() ); 130 os.close(); 131 clientSocket.close(); 132 } else { 133 134 os.write( new String ("+OK Give me five!").getBytes() ); 135 os.close(); 136 clientSocket.close(); 137 138 try { 139 140 SwingUtilities.invokeAndWait( new Runnable () 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 ("Unknow command: " + line); 166 } 167 169 } catch (Throwable tr) 170 { 171 tr.printStackTrace(); 172 } 174 } 175 }); 176 } catch (InterruptedException ex) { 177 ex.printStackTrace(); 178 } catch (InvocationTargetException ex) { 179 ex.printStackTrace(); 180 } 181 182 } 183 } catch (IOException e) { 184 } 185 } 186 187 188 } 189 190 191 193 public boolean runWizard(String 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 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 } 236 237 } catch (Exception 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); 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 269 public boolean ping() 270 { 271 return true; 272 } 273 274 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 290 public boolean openFile(String file) 291 { 292 setVisible(true); 293 try { 294 JReportFrame jrf = MainFrame.getMainInstance().openFile( file ); 295 jrf.setSelected(true); 296 return true; 297 } catch (Exception ex){ 298 return false; 299 } 300 } 301 } 302 | Popular Tags |