unification with the actual initial project. Some things are new now, but there has...
[RRRRHHHH_Code] / ruralHouses / src / businessLogic / BusinessLogicServer.java
1 package businessLogic;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.io.BufferedReader;
6 import java.io.InputStreamReader;
7 import java.rmi.Naming;
8 import java.rmi.RMISecurityManager;
9 import java.rmi.RemoteException;
10
11 import javax.swing.JButton;
12 import javax.swing.JDialog;
13 import javax.swing.JPanel;
14 import javax.swing.border.EmptyBorder;
15
16 import configuration.ConfigXML;
17
18 import javax.swing.JTextArea;
19
20 import dataAccess.DB4oManager;
21
22 import java.awt.event.ActionListener;
23 import java.awt.event.ActionEvent;
24
25 public class BusinessLogicServer extends JDialog {
26
27         private final JPanel contentPanel = new JPanel();
28         JTextArea textArea;
29         ApplicationFacadeInterface server;
30
31         /**
32          * Launch the application.
33          */
34         public static void main(String[] args) {
35                 try {
36                         BusinessLogicServer dialog = new BusinessLogicServer();
37                         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
38                         dialog.setVisible(true);
39                 } catch (Exception e) {
40                         e.printStackTrace();
41                 }
42         }
43
44         /**
45          * Create the dialog.
46          */
47         public BusinessLogicServer() {
48                 setTitle("BusinessLogicServer: running the business logic");
49                 setBounds(100, 100, 486, 209);
50                 getContentPane().setLayout(new BorderLayout());
51                 contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
52                 getContentPane().add(contentPanel, BorderLayout.CENTER);
53                 contentPanel.setLayout(new BorderLayout(0, 0));
54                 {
55                         textArea = new JTextArea();
56                         contentPanel.add(textArea);
57                 }
58                 {
59                         JPanel buttonPane = new JPanel();
60                         buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
61                         getContentPane().add(buttonPane, BorderLayout.SOUTH);
62                         {
63                                 JButton okButton = new JButton("OK");
64                                 okButton.addActionListener(new ActionListener() {
65                                         public void actionPerformed(ActionEvent e) {
66                                                 textArea.append("\n\n\nClosing the server... ");
67                                             try {
68                                                         server.close();
69                                                 } catch (RemoteException e1) {
70                                                 }
71                                                 System.exit(1);
72                                         }
73                                 });
74                                 okButton.setActionCommand("OK");
75                                 buttonPane.add(okButton);
76                                 getRootPane().setDefaultButton(okButton);
77                         }
78                         {
79                                 JButton cancelButton = new JButton("Cancel");
80                                 cancelButton.setActionCommand("Cancel");
81                                 buttonPane.add(cancelButton);
82                         }
83                 }
84                 
85                 ConfigXML c=ConfigXML.getInstance();
86
87                  
88                 System.setProperty("java.security.policy", c.getJavaPolicyPath());
89                 
90                 
91                 System.setSecurityManager(new RMISecurityManager());
92                 
93                 try {
94                         java.rmi.registry.LocateRegistry.createRegistry(Integer.parseInt(c.getPortRMI()));
95                         // Create RMIREGISTRY
96                 } catch (Exception e) {
97                         textArea.append(e.toString() + "Rmiregistry already running.");
98                 }
99
100                 try {
101
102                         try{
103                                 server = new FacadeImplementation();
104                         }
105                         catch (com.db4o.ext.DatabaseFileLockedException e) {
106                                 System.out.println("Error in BusinessLogicServer: "+e.toString());
107                                 textArea.append("\nYou should have not launched DB4oManagerServer...\n");
108                                 textArea.append("\n\nOr maybe there is a BusinessLogicServer already launched...\n");
109                                 throw e;
110                         }
111                         
112
113                         String service= "//"+c.getBusinessLogicNode() +":"+ c.getPortRMI()+"/"+c.getServiceRMI();
114                         
115                         // Register the remote server
116                         Naming.rebind(service, server);
117                         textArea.append("Running service at:\n\t" + service);
118                         //This operation removes the actual database and initialize with predefined values
119                         
120                         textArea.append("\n\n\nPress button to exit this server... ");
121                         
122                 } catch (Exception e) {
123                         textArea.append(e.toString());
124                 }
125
126         }
127                 
128 }
129