View Javadoc

1   package com.explosion.datastream.exql.processes;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import org.apache.log4j.LogManager;
23  import org.apache.log4j.Logger;
24  
25  import com.explosion.expfmodules.rdbmsconn.RdbmsConnModuleManager;
26  import com.explosion.expfmodules.rdbmsconn.connect.ConnectionManager;
27  import com.explosion.utilities.exception.ExceptionManagerFactory;
28  import com.explosion.utilities.preferences.groups.PreferenceGroup;
29  import com.explosion.utilities.process.StackableSimpleProcess;
30  import com.explosion.utilities.process.threads.Finishable;
31  import com.explosion.utilities.process.threads.ProcessThread;
32  
33  /***
34   * @author Stephen Cowx
35   * Date created:@14-Feb-2003
36   * This class connects to a database.
37   */
38  public class ConnectProcess extends StackableSimpleProcess
39  {
40    private static Logger log = LogManager.getLogger(ConnectProcess.class);
41    private PreferenceGroup connectionDescriptor;
42    private int connectionIdentifier = -1;
43    
44    /***
45     * Constructor for RefreshTopNodesProcess.
46     */
47    public ConnectProcess(Finishable finishable,PreferenceGroup connectionDescriptor)
48    {
49      super(finishable, null);
50      this.connectionDescriptor = connectionDescriptor;
51      this.setIsUserProcess(true);
52    }
53    
54    public void process()
55    {
56    	try
57    	{
58    	    if (isStopped()) return;
59  	  	setStatusText("Connecting");
60  	  	
61  	  	setPercentComplete(50);
62  
63  	  	connectionIdentifier = ConnectionManager.getInstance().connect(connectionDescriptor, RdbmsConnModuleManager.instance().getDriverDescriptorManager());
64          
65          if (connectionIdentifier < 0)
66            setStatusText("Done, unable to connect.");
67  	    else
68  	      setStatusText("Done, connected successfully.");
69  	      
70  	    this.setPercentComplete(100);
71  	    log.debug(getStatusText()); 
72    	}
73      catch (Exception e)
74      {
75          setStatusText("Error, unable to connect.");
76          setPercentComplete(100);
77          if (getProcessControl().getStatus() != ProcessThread.THREAD_STOPPED)
78      	{ 
79      	  //ie, it hasn't been cancelled
80            ExceptionManagerFactory.getExceptionManager().manageException( e, "Exception caught while connecting to database.");
81      	}
82      	
83      }
84  
85    }
86  
87    /***
88     * Returns the id of the connection created.
89     * @return DataSet
90     */
91    public int getConnectionIdentifier()
92    {
93      return connectionIdentifier;
94    }
95  
96  }