106cfff349b65e8b453f018004c755f4478fc29e
[RRRRHHHH_Code] / ruralHouses / src / dataAccess / DB4oManagerServer.java
1 package dataAccess;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.io.File;
8
9 import javax.swing.JButton;
10 import javax.swing.JDialog;
11 import javax.swing.JPanel;
12 import javax.swing.JTextArea;
13 import javax.swing.border.EmptyBorder;
14
15 import com.db4o.ObjectServer;
16 import com.db4o.cs.Db4oClientServer;
17 import com.db4o.cs.config.ServerConfiguration;
18
19 import configuration.ConfigXML;
20 import domain.Owner;
21
22 public class DB4oManagerServer extends JDialog {
23
24         /**
25          * 
26          */
27         private static final long serialVersionUID = 1L;
28         private final JPanel contentPanel = new JPanel();
29         JTextArea textArea;
30         ObjectServer server;
31         private ServerConfiguration configurationCS;
32
33         /**
34          * Launch the application.
35          */
36         public static void main(String[] args) {
37                 try {
38                         DB4oManagerServer dialog = new DB4oManagerServer();
39                         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
40                         dialog.setVisible(true);
41                 } catch (Exception e) {
42                         e.printStackTrace();
43                 }
44         }
45
46         /**
47          * Create the dialog.
48          */
49         public DB4oManagerServer() {
50                 setTitle("DB4oManagerServer: running the database server");
51                 setBounds(100, 100, 486, 180);
52                 getContentPane().setLayout(new BorderLayout());
53                 contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
54                 getContentPane().add(contentPanel, BorderLayout.CENTER);
55                 contentPanel.setLayout(new BorderLayout(0, 0));
56                 {
57                         textArea = new JTextArea();
58                         contentPanel.add(textArea);
59                 }
60                 {
61                         JPanel buttonPane = new JPanel();
62                         buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
63                         getContentPane().add(buttonPane, BorderLayout.SOUTH);
64                         {
65                                 JButton okButton = new JButton("OK");
66                                 okButton.addActionListener(new ActionListener() {
67                                         public void actionPerformed(ActionEvent e) {
68                                                 textArea.append("\n\n\nClosing the database... ");
69                                             try {
70                                                         server.close();
71                                                 } catch (Exception e1) {
72                                                 }
73                                                 System.exit(1);
74                                         }
75                                 });
76                                 okButton.setActionCommand("OK");
77                                 buttonPane.add(okButton);
78                                 getRootPane().setDefaultButton(okButton);
79                         }
80                         {
81                                 JButton cancelButton = new JButton("Cancel");
82                                 cancelButton.setActionCommand("Cancel");
83                                 buttonPane.add(cancelButton);
84                         }
85                 }
86                 
87                 ConfigXML c=ConfigXML.getInstance();
88                 
89                 
90                 try{
91
92                         if (c.getDataBaseOpenMode().equals("initialize")) new File(c.getDb4oFilename()).delete();
93                         
94                         configurationCS = Db4oClientServer.newServerConfiguration();
95                         configurationCS.common().activationDepth(c.getActivationDepth());
96                         configurationCS.common().updateDepth(c.getUpdateDepth());
97                         configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true);
98
99                         server = Db4oClientServer.openServer(configurationCS,
100                                                                                                  c.getDb4oFilename(),c.getDatabasePort());
101                         
102                         textArea.append("\nConnection to the database '"+c.getDb4oFilename()+"' opened in port "+c.getDatabasePort());
103         
104                     server.grantAccess(c.getUser(),c.getPassword());
105
106                     textArea.append("\nAccess granted to: "+c.getUser());
107
108
109
110                         textArea.append("\nPress button to exit this database server... ");
111                         
112                 } catch (Exception e) {
113                         textArea.append("Something has happened in DB4oManagerServer: "+e.toString());
114
115                 }
116         }
117 }
118