1
14 package gate.util.persistence;
15
16 import java.util.Map;
17
18 import javax.swing.*;
19
20 import junit.framework.Assert;
21
22 import gate.*;
23 import gate.creole.ResourceInstantiationException;
24 import gate.gui.OkCancelDialog;
25 import gate.persist.JDBCDataStore;
26 import gate.persist.PersistenceException;
27 import gate.security.*;
28
31 public class JDBCDSPersistence extends DSPersistence {
32
33
37 public void extractDataFromSource(Object source)throws PersistenceException{
38 if(! (source instanceof JDBCDataStore)){
40 throw new UnsupportedOperationException(
41 getClass().getName() + " can only be used for " +
42 JDBCDataStore.class.getName() +
43 " objects!\n" + source.getClass().getName() +
44 " is not a " + JDBCDataStore.class.getName());
45 }
46
47 super.extractDataFromSource(source);
48
49 JDBCDataStore ds = (JDBCDataStore)source;
50 Map securityData = DataStoreRegister.getSecurityData(ds);
51 userName = ((User)securityData.get("user")).getName();
52 userGroup = ((Group)securityData.get("group")).getName();
53 }
54
55
56
60 public Object createObject()throws PersistenceException,
61 ResourceInstantiationException{
62
63 AccessController ac = null;
64 JDBCDataStore ds = null;
65 User usr = null;
66 Group grp = null;
67
68 DataStoreRegister reg = Gate.getDataStoreRegister();
69 boolean securityOK = false;
70 Session mySession = null;
71 securityLoop: do{
73 try{
74 String userPass;
75 ac = new AccessControllerImpl(storageUrlString);
76 ac = Factory.createAccessController(storageUrlString);
77 Assert.assertNotNull(ac);
78 ac.open();
79
80 try {
81 Box listBox = Box.createHorizontalBox();
82
83 Box vBox = Box.createVerticalBox();
84 vBox.add(new JLabel("User name: "));
85 vBox.add(new JLabel("Password: "));
86 vBox.add(new JLabel("Group: "));
87 listBox.add(vBox);
88 listBox.add(Box.createHorizontalStrut(20));
89
90 JPanel panel2 = new JPanel();
91 panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
92 vBox = Box.createVerticalBox();
93
94 JTextField usrField = new JTextField(30);
95 usrField.setText(userName);
96 vBox.add(usrField);
97 JPasswordField pwdField = new JPasswordField(30);
98 vBox.add(pwdField);
99 JTextField grpField = new JTextField(30);
100 grpField.setText(userGroup);
101 vBox.add(grpField);
102
103 listBox.add(vBox);
104
105 if(OkCancelDialog.showDialog(null, listBox,
106 "Please re-enter login details")){
107 userName = usrField.getText();
108 userPass = new String(pwdField.getPassword());
109 userGroup = grpField.getText();
110 if (userName.equals("") || userPass.equals("") || userGroup.equals("")) {
111 JOptionPane.showMessageDialog(
112 null,
113 "You must provide non-empty user name, password and group!",
114 "Login error",
115 JOptionPane.ERROR_MESSAGE
116 );
117 securityOK = false;
118 continue securityLoop;
119 }
120 }else{
121 try {
123 if (ac != null)
124 ac.close();
125 if (ds != null)
126 ds.close();
127 } catch (gate.persist.PersistenceException ex) {
128 JOptionPane.showMessageDialog(null, "Persistence error!\n " +
129 ex.toString(),
130 "GATE", JOptionPane.ERROR_MESSAGE);
131 }
132 throw new PersistenceException("User cancelled!");
133 }
134
135 grp = ac.findGroup(userGroup);
136 usr = ac.findUser(userName);
137 mySession = ac.login(userName, userPass, grp.getID());
138 } catch (gate.security.SecurityException ex) {
139 JOptionPane.showMessageDialog(
140 null,
141 "Authentication failed! Incorrect details entred.",
142 "Login error",
143 JOptionPane.ERROR_MESSAGE
144 );
145 securityOK = false;
146 continue securityLoop;
147 }
148
149 if (! ac.isValidSession(mySession)){
150 JOptionPane.showMessageDialog(
151 null,
152 "Incorrect session obtained. "
153 + "Probably there is a problem with the database!",
154 "Login error",
155 JOptionPane.ERROR_MESSAGE
156 );
157 securityOK = false;
158 continue securityLoop;
159 }
160 }catch(gate.security.SecurityException se) {
161 JOptionPane.showMessageDialog(null, "User identification error!\n " +
162 se.toString(),
163 "GATE", JOptionPane.ERROR_MESSAGE);
164 securityOK = false;
165 continue securityLoop;
166 }
167 securityOK = true;
168 } while(!securityOK);
169
170 try {
171
172 ds = (JDBCDataStore)super.createObject();
174 try {
175 ds.setSession(mySession);
176 } catch(gate.security.SecurityException ex1) {
177 throw new PersistenceException(ex1.getMessage());
178 }
179
180 FeatureMap securityData = Factory.newFeatureMap();
184 securityData.put("user", usr);
185 securityData.put("group", grp);
186 DataStoreRegister.addSecurityData(ds, securityData);
187
188 } catch(PersistenceException pe) {
189 JOptionPane.showMessageDialog(null, "Datastore open error!\n " +
190 pe.toString(),
191 "GATE", JOptionPane.ERROR_MESSAGE);
192 }
193
194 return ds;
195 }
196
197 protected String userName;
198 protected String userGroup;
199 }