001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5;
043:
044: import java.io.BufferedInputStream;
045: import java.io.BufferedOutputStream;
046: import java.io.File;
047: import java.io.FileOutputStream;
048: import java.io.IOException;
049: import java.net.URISyntaxException;
050: import java.net.URL;
051: import java.util.ArrayList;
052: import java.util.Arrays;
053: import java.util.Collection;
054: import java.util.List;
055: import java.util.Set;
056: import java.util.logging.Level;
057: import java.util.logging.Logger;
058: import javax.enterprise.deploy.shared.ActionType;
059: import javax.enterprise.deploy.shared.CommandType;
060: import javax.enterprise.deploy.shared.StateType;
061: import javax.enterprise.deploy.spi.Target;
062: import javax.enterprise.deploy.spi.TargetModuleID;
063: import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException;
064: import javax.enterprise.deploy.spi.status.ClientConfiguration;
065: import javax.enterprise.deploy.spi.status.DeploymentStatus;
066: import javax.enterprise.deploy.spi.status.ProgressListener;
067: import javax.enterprise.deploy.spi.status.ProgressObject;
068: import org.netbeans.api.db.explorer.DatabaseConnection;
069: import org.netbeans.api.db.explorer.JDBCDriver;
070: import org.netbeans.api.db.explorer.JDBCDriverManager;
071: import org.netbeans.modules.j2ee.common.DatasourceHelper;
072: import org.netbeans.modules.j2ee.common.Util;
073: import org.netbeans.modules.j2ee.deployment.common.api.Datasource;
074: import org.netbeans.modules.j2ee.deployment.plugins.spi.JDBCDriverDeployer;
075: import org.netbeans.modules.tomcat5.progress.ProgressEventSupport;
076: import org.netbeans.modules.tomcat5.progress.Status;
077: import org.netbeans.modules.tomcat5.util.TomcatProperties;
078: import org.openide.filesystems.FileObject;
079: import org.openide.filesystems.FileUtil;
080: import org.openide.filesystems.URLMapper;
081: import org.openide.util.NbBundle;
082: import org.openide.util.RequestProcessor;
083:
084: /**
085: * Tomcat JDBCDriverDeployer implementation.
086: *
087: * @author sherold
088: */
089: public class TomcatJDBCDriverDeployer implements JDBCDriverDeployer {
090:
091: private static final Logger LOG = Logger
092: .getLogger(TomcatJDBCDriverDeployer.class.getName());
093:
094: private final TomcatManager manager;
095:
096: /** Creates a new instance of TomcatJDBCDriverDeployer */
097: public TomcatJDBCDriverDeployer(TomcatManager manager) {
098: this .manager = manager;
099: }
100:
101: public boolean supportsDeployJDBCDrivers(Target target) {
102: return manager.getTomcatProperties().getDriverDeployment();
103: }
104:
105: public ProgressObject deployJDBCDrivers(Target target,
106: Set<Datasource> datasources) {
107: return new DriverDeploymentProgressObject(datasources);
108: }
109:
110: private class DriverDeploymentProgressObject implements
111: ProgressObject, Runnable {
112:
113: private final ProgressEventSupport eventSupport;
114: private final Set<Datasource> datasources;
115:
116: public DriverDeploymentProgressObject(
117: Set<Datasource> datasources) {
118: eventSupport = new ProgressEventSupport(
119: TomcatJDBCDriverDeployer.this );
120: this .datasources = datasources;
121: String msg = NbBundle.getMessage(
122: TomcatJDBCDriverDeployer.class,
123: "MSG_CheckingMissingDrivers");
124: eventSupport.fireHandleProgressEvent(null, new Status(
125: ActionType.EXECUTE, CommandType.DISTRIBUTE, msg,
126: StateType.RUNNING));
127: RequestProcessor.getDefault().post(this );
128: }
129:
130: public void run() {
131: List<FileObject> jdbcDriverFiles = jdbcDriversToDeploy();
132: // deploy the driers if needed
133: if (!jdbcDriverFiles.isEmpty()) {
134: TomcatProperties tp = manager.getTomcatProperties();
135: for (FileObject file : jdbcDriverFiles) {
136: File libsDir = tp.getLibsDir();
137: File toJar = new File(libsDir, file.getNameExt());
138: try {
139: BufferedInputStream is = new BufferedInputStream(
140: file.getInputStream());
141: try {
142: String msg = NbBundle.getMessage(
143: TomcatJDBCDriverDeployer.class,
144: "MSG_DeployingJDBCDrivers", toJar
145: .getPath());
146: eventSupport.fireHandleProgressEvent(null,
147: new Status(ActionType.EXECUTE,
148: CommandType.DISTRIBUTE,
149: msg, StateType.RUNNING));
150: BufferedOutputStream os = new BufferedOutputStream(
151: new FileOutputStream(toJar));
152: try {
153: FileUtil.copy(is, os);
154: } finally {
155: os.close();
156: }
157: } finally {
158: is.close();
159: }
160: } catch (IOException e) {
161: LOG.log(Level.INFO, null, e);
162: String msg = NbBundle.getMessage(
163: TomcatJDBCDriverDeployer.class,
164: "MSG_DeployingJDBCDriversFailed", toJar
165: .getPath(), libsDir.getPath());
166: eventSupport.fireHandleProgressEvent(null,
167: new Status(ActionType.EXECUTE,
168: CommandType.DISTRIBUTE, msg,
169: StateType.FAILED));
170: return;
171: }
172: }
173: // set the restart flag
174: manager.setNeedsRestart(true);
175: }
176: eventSupport.fireHandleProgressEvent(null, new Status(
177: ActionType.EXECUTE, CommandType.DISTRIBUTE, "",
178: StateType.COMPLETED)); // NOI18N
179: }
180:
181: /** Returns a list of jdbc drivers that need to be deployed. */
182: private List<FileObject> jdbcDriversToDeploy() {
183: List<FileObject> jdbcDriverFiles = new ArrayList<FileObject>();
184: Collection<File> driverCP = getJDBCDriverClasspath();
185: for (Datasource datasource : datasources) {
186: String className = datasource.getDriverClassName();
187: boolean exists = false;
188: try {
189: exists = Util.containsClass(driverCP, className);
190: } catch (IOException e) {
191: LOG.log(Level.INFO, null, e);
192: }
193: if (!exists) {
194: for (DatabaseConnection databaseConnection : DatasourceHelper
195: .findDatabaseConnections(datasource)) {
196: String driverClass = databaseConnection
197: .getDriverClass();
198: JDBCDriver[] jdbcDrivers = JDBCDriverManager
199: .getDefault().getDrivers(driverClass);
200: for (JDBCDriver jdbcDriver : jdbcDrivers) {
201: for (URL url : jdbcDriver.getURLs()) {
202: FileObject file = URLMapper
203: .findFileObject(url);
204: if (file != null) {
205: jdbcDriverFiles.add(file);
206: }
207: }
208: }
209: }
210: }
211: }
212: return jdbcDriverFiles;
213: }
214:
215: /** Returns a classpath where the JDBC drivers could be placed */
216: private Collection<File> getJDBCDriverClasspath() {
217: TomcatProperties tp = manager.getTomcatProperties();
218: return Arrays.asList(tp.getLibsDir().listFiles());
219: }
220:
221: public DeploymentStatus getDeploymentStatus() {
222: return eventSupport.getDeploymentStatus();
223: }
224:
225: public TargetModuleID[] getResultTargetModuleIDs() {
226: return null;
227: }
228:
229: public ClientConfiguration getClientConfiguration(
230: TargetModuleID targetModuleID) {
231: return null;
232: }
233:
234: public boolean isCancelSupported() {
235: return false;
236: }
237:
238: public void cancel() throws OperationUnsupportedException {
239: throw new OperationUnsupportedException(
240: "Cancel is not supported"); // NOI18N
241: }
242:
243: public boolean isStopSupported() {
244: return false;
245: }
246:
247: public void stop() throws OperationUnsupportedException {
248: throw new OperationUnsupportedException(
249: "Stop is not supported"); // NOI18N
250: }
251:
252: public void addProgressListener(
253: ProgressListener progressListener) {
254: eventSupport.addProgressListener(progressListener);
255: }
256:
257: public void removeProgressListener(
258: ProgressListener progressListener) {
259: eventSupport.removeProgressListener(progressListener);
260: }
261:
262: }
263: }
|