View Javadoc

1   /* ==============================================================================
2    *   JDBCAdmin, data management software.
3    *   Copyright (C) 2005  Norsys S.A
4    *
5    *   This library is free software; you can redistribute it and/or
6    *   modify it under the terms of the GNU Lesser General Public
7    *   License as published by the Free Software Foundation; either
8    *   version 2.1 of the License, or (at your option) any later version.
9    *
10   *   This library is distributed in the hope that it will be useful,
11   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *   Lesser General Public License for more details.
14   *
15   *   You should have received a copy of the GNU Lesser General Public
16   *   License along with this library; if not, write to the Free Software
17   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * ==============================================================================
19   */
20  package jdbcadmin.web.actions;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import org.apache.struts.action.Action;
29  import org.apache.struts.action.ActionError;
30  import org.apache.struts.action.ActionErrors;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  
35  import jdbcadmin.core.data.ConnectionInfo;
36  import jdbcadmin.web.ContexteManager;
37  import jdbcadmin.web.forms.ConnectionForm;
38  
39  /***
40   * Connection Action.
41   * @struts.action
42   * name="connectionForm"
43   * path="/connection"
44   * @struts.action-forward
45   * name="main"
46   * path="/listSchema.do"
47   * @author Thomas Recloux (trecloux@norsys.fr)
48   */
49  public class ConnectionAction extends Action {
50  
51      /*** logger */
52      private static Log logger = LogFactory.getLog(ConnectionAction.class);
53  
54      /***
55       *  {@inheritDoc}
56       */
57      public ActionForward execute(
58          ActionMapping aMapping,
59          ActionForm aForm,
60          HttpServletRequest aRequest,
61          HttpServletResponse aResponse)
62          throws Exception {
63  
64          String forward = "main";
65          ActionErrors errors = new ActionErrors();
66  
67          try {
68              if (aRequest.getSession().getAttribute(ContexteManager.RESTRICTION_KEY) != null) {
69  
70                  ConnectionForm form = (ConnectionForm) aForm;
71                  ConnectionInfo cnxInfos = new ConnectionInfo();
72                  String jndiName = form.getJndiName();
73                  if (jndiName != null && !jndiName.equals("")) {
74                      cnxInfos.setDataSourceJndiName(form.getJndiName());
75                  }
76                  cnxInfos.setUserName(form.getUserName());
77                  cnxInfos.setUserPassword(form.getUserPassword());
78                  cnxInfos.setUrl(form.getUrl());
79                  cnxInfos.setDriverName(form.getDriverName());
80                  if (form.getDriverJar() != null) {
81                      cnxInfos.setDriverJarData(form.getDriverJar().getFileData());
82                  }
83  
84                  ContexteManager.setCnxInfos(aRequest, cnxInfos);
85              } else {
86                  forward = "index";
87              }
88  
89          } catch (Exception e) {
90              if (logger.isErrorEnabled()) {
91                  logger.error("Unexpected error during the connection", e);
92              }
93              errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.generic", e.getMessage()));
94              forward = "error";
95          }
96  
97          if (!errors.isEmpty()) {
98              saveErrors(aRequest, errors);
99          }
100         return aMapping.findForward(forward);
101 
102     }
103 
104 }