JDBCDSPersistence.java
001 /*
002  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
003  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004  *
005  *  This file is part of GATE (see http://gate.ac.uk/), and is free
006  *  software, licenced under the GNU Library General Public License,
007  *  Version 2, June 1991 (in the distribution as file licence.html,
008  *  and also available at http://gate.ac.uk/gate/licence.html).
009  *
010  *  Valentin Tablan 26/10/2001
011  *
012  *  $Id: JDBCDSPersistence.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013  *
014  */
015 package gate.util.persistence;
016 
017 import java.util.Map;
018 
019 import javax.swing.*;
020 
021 import junit.framework.Assert;
022 
023 import gate.*;
024 import gate.creole.ResourceInstantiationException;
025 import gate.gui.MainFrame;
026 import gate.gui.OkCancelDialog;
027 import gate.persist.JDBCDataStore;
028 import gate.persist.PersistenceException;
029 import gate.security.*;
030 /**
031  * Adds security data storage to the DS persistence
032  */
033 public class JDBCDSPersistence extends DSPersistence {
034 
035   /**
036    * Populates this Persistence with the data that needs to be stored from the
037    * original source object.
038    */
039   public void extractDataFromSource(Object source)throws PersistenceException{
040     //check input
041     if((source instanceof JDBCDataStore)){
042       throw new UnsupportedOperationException(
043                 getClass().getName() " can only be used for " +
044                 JDBCDataStore.class.getName() +
045                 " objects!\n" + source.getClass().getName() +
046                 " is not a " + JDBCDataStore.class.getName());
047     }
048 
049     super.extractDataFromSource(source);
050 
051     JDBCDataStore ds = (JDBCDataStore)source;
052     Map securityData = DataStoreRegister.getSecurityData(ds);
053     userName = ((User)securityData.get("user")).getName();
054     userGroup = ((Group)securityData.get("group")).getName();
055   }
056 
057 
058   /**
059    * Creates a new object from the data contained. This new object is supposed
060    * to be a copy for the original object used as source for data extraction.
061    */
062   public Object createObject()throws PersistenceException,
063                                      ResourceInstantiationException{
064 
065     AccessController ac = null;
066     JDBCDataStore ds = null;
067     User usr = null;
068     Group grp = null;
069 
070     DataStoreRegister reg = Gate.getDataStoreRegister();
071     boolean securityOK = false;
072     Session mySession = null;
073     //1. login the user;
074     securityLoop: do{
075       try{
076         String userPass;
077         ac = new AccessControllerImpl(storageUrlString);
078         ac = Factory.createAccessController(storageUrlString);
079         Assert.assertNotNull(ac);
080         ac.open();
081 
082         try {
083           Box listBox = Box.createHorizontalBox();
084 
085           Box vBox = Box.createVerticalBox();
086           vBox.add(new JLabel("User name: "));
087           vBox.add(new JLabel("Password: "));
088           vBox.add(new JLabel("Group: "));
089           listBox.add(vBox);
090           listBox.add(Box.createHorizontalStrut(20));
091 
092           JPanel panel2 = new JPanel();
093           panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
094           vBox = Box.createVerticalBox();
095 
096           JTextField usrField = new JTextField(30);
097           usrField.setText(userName);
098           vBox.add(usrField);
099           JPasswordField pwdField = new JPasswordField(30);
100           vBox.add(pwdField);
101           JTextField grpField = new JTextField(30);
102           grpField.setText(userGroup);
103           vBox.add(grpField);
104 
105           listBox.add(vBox);
106 
107           if(OkCancelDialog.showDialog(null, listBox,
108                                        "Please re-enter login details")){
109             userName = usrField.getText();
110             userPass = new String(pwdField.getPassword());
111             userGroup = grpField.getText();
112             if (userName.equals(""|| userPass.equals(""|| userGroup.equals("")) {
113               JOptionPane.showMessageDialog(
114                 null,
115                 "You must provide non-empty user name, password and group!",
116                 "Login error",
117                 JOptionPane.ERROR_MESSAGE
118                 );
119               securityOK = false;
120               continue securityLoop;
121             }
122           }else{
123             //user cancelled
124             try {
125               if (ac != null)
126                 ac.close();
127               if (ds != null)
128                 ds.close();
129             catch (gate.persist.PersistenceException ex) {
130               JOptionPane.showMessageDialog(MainFrame.getInstance()"Persistence error!\n " +
131                                             ex.toString(),
132                                             "GATE", JOptionPane.ERROR_MESSAGE);
133             }
134             throw new PersistenceException("User cancelled!");
135           }
136 
137           grp = ac.findGroup(userGroup);
138           usr = ac.findUser(userName);
139           mySession = ac.login(userName, userPass, grp.getID());
140         catch (gate.security.SecurityException ex) {
141             JOptionPane.showMessageDialog(
142               null,
143               "Authentication failed! Incorrect details entred.",
144               "Login error",
145               JOptionPane.ERROR_MESSAGE
146               );
147           securityOK = false;
148           continue securityLoop;
149         }
150 
151         if (! ac.isValidSession(mySession)){
152           JOptionPane.showMessageDialog(
153             null,
154             "Incorrect session obtained. "
155               "Probably there is a problem with the database!",
156             "Login error",
157             JOptionPane.ERROR_MESSAGE
158             );
159           securityOK = false;
160           continue securityLoop;
161         }
162       }catch(gate.security.SecurityException se) {
163         JOptionPane.showMessageDialog(MainFrame.getInstance()"User identification error!\n " +
164                                       se.toString(),
165                                       "GATE", JOptionPane.ERROR_MESSAGE);
166         securityOK = false;
167         continue securityLoop;
168       }
169       securityOK = true;
170     while(!securityOK);
171 
172     try {
173 
174       //2. open the oracle datastore
175       ds = (JDBCDataStore)super.createObject();
176       try {
177         ds.setSession(mySession);
178       catch(gate.security.SecurityException ex1) {
179         throw new PersistenceException(ex1.getMessage());
180       }
181 
182       //3. add the security data for this datastore
183       //this saves the user and group information, so it can
184       //be used later when resources are created with certain rights
185       FeatureMap securityData = Factory.newFeatureMap();
186       securityData.put("user", usr);
187       securityData.put("group", grp);
188       DataStoreRegister.addSecurityData(ds, securityData);
189 
190     catch(PersistenceException pe) {
191       JOptionPane.showMessageDialog(MainFrame.getInstance()"Datastore open error!\n " +
192                                     pe.toString(),
193                                     "GATE", JOptionPane.ERROR_MESSAGE);
194     }
195 
196     return ds;
197   }
198 
199   protected String userName;
200   protected String userGroup;
201 }