001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * IReportTCPServer.java
028: *
029: * Created on August 17, 2006, 3:00 PM
030: *
031: */
032:
033: package it.businesslogic.ireport.rmi;
034:
035: import it.businesslogic.ireport.Report;
036: import it.businesslogic.ireport.gui.JReportFrame;
037: import it.businesslogic.ireport.gui.MainFrame;
038: import it.businesslogic.ireport.gui.WizardDialog;
039: import it.businesslogic.ireport.util.PageSize;
040: import java.io.DataInputStream;
041: import java.io.IOException;
042: import java.io.PrintStream;
043: import java.lang.reflect.InvocationTargetException;
044: import java.net.ServerSocket;
045: import java.net.Socket;
046: import javax.swing.SwingUtilities;
047:
048: /**
049: *
050: * @author gtoffoli
051: */
052: public class IReportTCPServer implements Runnable {
053:
054: static IReportTCPServer mainInstance = null;
055:
056: public static IReportTCPServer getMainInstance() {
057: if (mainInstance == null) {
058: try {
059: mainInstance = new IReportTCPServer();
060: } catch (Exception ex) {
061: ex.printStackTrace();
062: }
063: }
064:
065: return mainInstance;
066: }
067:
068: /** Creates a new instance of IReportTCPServer */
069: public IReportTCPServer() {
070: }
071:
072: public static void runServer() {
073: if (MainFrame.getMainInstance().getProperties().getProperty(
074: "enableRMIServer", "false").equals("true")) {
075: Thread t = new Thread(IReportTCPServer.getMainInstance());
076: t.start();
077: //SwingUtilities.invokeLater( IReportServerImpl.getMainInstance() );
078: }
079: }
080:
081: public void run() {
082:
083: ServerSocket serverSocket = null;
084: DataInputStream is;
085: PrintStream os;
086:
087: Socket clientSocket = null;
088:
089: int port = 2100;
090: try {
091: port = Integer.parseInt(MainFrame.getMainInstance()
092: .getProperties().getProperty("RMIServerPort",
093: "2100"));
094: } catch (Exception ex) {
095: ex.printStackTrace();
096: }
097:
098: try {
099:
100: serverSocket = new ServerSocket(port);
101: MainFrame.getMainInstance().logOnConsole(
102: "Demone listening on port: "
103: + serverSocket.getLocalPort());
104:
105: } catch (IOException e) {
106:
107: MainFrame.getMainInstance().logOnConsole(
108: "Error opening the socket : " + e.getMessage());
109: return;
110: }
111:
112: // Non ci interessa gestire connessioni multiple...
113: while (true) {
114: is = null;
115: os = null;
116: clientSocket = null;
117: try {
118: clientSocket = serverSocket.accept();
119: is = new DataInputStream(clientSocket.getInputStream());
120:
121: final String line = is.readLine();
122: os = new PrintStream(clientSocket.getOutputStream());
123:
124: if (line == null) {
125: os.write(new String("-Unknow command!").getBytes());
126: os.close();
127: clientSocket.close();
128: } else {
129:
130: os
131: .write(new String("+OK Give me five!")
132: .getBytes());
133: os.close();
134: clientSocket.close();
135:
136: try {
137:
138: SwingUtilities.invokeAndWait(new Runnable() {
139: public void run() {
140:
141: try {
142: if (line.toUpperCase().startsWith(
143: "PING")) {
144:
145: } else if (line.toUpperCase()
146: .startsWith("WIZARD ")) {
147: runWizard(line.substring(7));
148: } else if (line.toUpperCase()
149: .startsWith("OPENFILE ")) {
150: openFile(line.substring(9));
151: } else if (line.toUpperCase()
152: .startsWith("SETVISIBLE ")) {
153: setVisible(Boolean.valueOf(
154: line.substring(11)
155: .trim())
156: .booleanValue());
157: } else {
158: throw new Exception(
159: "Unknow command: "
160: + line);
161: }
162: //os.write( new String("+OK Give me five!").getBytes() );
163:
164: } catch (Throwable tr) {
165: tr.printStackTrace();
166: //os.write( new String("-Error " + tr).getBytes() );
167: }
168: }
169: });
170: } catch (InterruptedException ex) {
171: ex.printStackTrace();
172: } catch (InvocationTargetException ex) {
173: ex.printStackTrace();
174: }
175:
176: }
177: } catch (IOException e) {
178: }
179: }
180:
181: }
182:
183: // ---------------------------------------------------------------------------------
184:
185: public boolean runWizard(String destFile) {
186: MainFrame mainFrame = MainFrame.getMainInstance();
187:
188: if (mainFrame == null)
189: return false;
190:
191: mainFrame.logOnConsole("Invocato wizard");
192: mainFrame
193: .logOnConsole("Pronto ad invocare la nuova finestra..."
194: + Thread.currentThread().getName());
195:
196: try {
197: // TODO
198: // Set the project directory as current directory;
199:
200: WizardDialog wd = new WizardDialog(mainFrame, true);
201:
202: mainFrame.logOnConsole("Lancio wizard");
203: wd.setVisible(true);
204: wd.requestFocus();
205:
206: Report report = null;
207: if (wd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
208: report = wd.getReport();
209: if (report == null) {
210: report = createBlankReport();
211: }
212: } else {
213: report = createBlankReport();
214: }
215:
216: if (report != null) {
217: mainFrame.openNewReportWindow(report);
218: report.setFilename(destFile);
219: report.saveXMLFile();
220: //setVisible(true);
221: }
222:
223: } catch (Exception ex) {
224: System.out.println(ex.getMessage());
225: ex.printStackTrace();
226: }
227:
228: return true;
229: }
230:
231: private Report createBlankReport() {
232: Report newReport = new Report();
233:
234: newReport.setName(it.businesslogic.ireport.util.I18n.getString(
235: "untitledReport", "untitled_report_")
236: + "1");
237: newReport.setUsingMultiLineExpressions(false); //this.isUsingMultiLineExpressions());
238: newReport.setWidth(PageSize.A4.x);
239: newReport.setHeight(PageSize.A4.y);
240: newReport.setTopMargin(20);
241: newReport.setLeftMargin(30);
242: newReport.setRightMargin(30);
243: newReport.setBottomMargin(20);
244: newReport.setColumnCount(1);
245: newReport.setColumnWidth(newReport.getWidth()
246: - newReport.getLeftMargin()
247: - newReport.getRightMargin());
248: newReport.setColumnSpacing(0);
249:
250: return newReport;
251: }
252:
253: /**
254: * Used to check if iReport is alive
255: */
256: public boolean ping() {
257: return true;
258: }
259:
260: /**
261: * Used to show the main window and bring the iReport window on top...
262: */
263: public boolean setVisible(boolean b) {
264: MainFrame.getMainInstance().setVisible(b);
265: if (MainFrame.getMainInstance().getState() == java.awt.Frame.ICONIFIED) {
266: MainFrame.getMainInstance().setState(java.awt.Frame.NORMAL);
267: }
268: return MainFrame.getMainInstance().requestFocusInWindow();
269: }
270:
271: /**
272: * Open the file passed as parameter...
273: */
274: public boolean openFile(String file) {
275: setVisible(true);
276: try {
277: JReportFrame jrf = MainFrame.getMainInstance().openFile(
278: file);
279: jrf.setSelected(true);
280: return true;
281: } catch (Exception ex) {
282: return false;
283: }
284: }
285: }
|