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.j2ee.ejbjarproject;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import javax.swing.JComboBox;
047: import org.netbeans.modules.j2ee.common.DatasourceUIHelper;
048: import org.netbeans.modules.j2ee.deployment.common.api.ConfigurationException;
049: import org.netbeans.modules.j2ee.deployment.common.api.Datasource;
050: import org.netbeans.modules.j2ee.persistence.spi.datasource.JPADataSource;
051: import org.netbeans.modules.j2ee.persistence.spi.datasource.JPADataSourcePopulator;
052: import org.netbeans.modules.j2ee.persistence.spi.datasource.JPADataSourceProvider;
053:
054: /**
055: * Implements the SPI interfaces providing support for data source handling.
056: *
057: * @author Erno Mononen
058: */
059: public class EjbJarJPASupport implements JPADataSourcePopulator,
060: JPADataSourceProvider {
061:
062: private final EjbJarProject project;
063:
064: /** Creates a new instance of EjbJarJPASupport */
065: public EjbJarJPASupport(EjbJarProject project) {
066: this .project = project;
067: }
068:
069: public void connect(JComboBox comboBox) {
070: DatasourceUIHelper.connect(project.getEjbModule(), comboBox);
071: }
072:
073: public List<JPADataSource> getDataSources() {
074:
075: List<Datasource> datasources = new ArrayList<Datasource>();
076: try {
077: datasources.addAll(project.getEjbModule()
078: .getModuleDatasources());
079: } catch (ConfigurationException e) {
080: // TODO: it would be reasonable to rethrow this exception, see #96791
081: }
082: try {
083: datasources.addAll(project.getEjbModule()
084: .getServerDatasources());
085: } catch (ConfigurationException e) {
086: // TODO: it would be reasonable to rethrow this exception, see #96791
087: }
088:
089: List<JPADataSource> result = new ArrayList<JPADataSource>(
090: datasources.size());
091: for (Datasource each : datasources) {
092: result.add(new DatasourceWrapper(each));
093: }
094: return result;
095: }
096:
097: public JPADataSource toJPADataSource(Object dataSource) {
098: if (dataSource instanceof JPADataSource) {
099: return (JPADataSource) dataSource;
100: } else if (dataSource instanceof Datasource) {
101: return new DatasourceWrapper((Datasource) dataSource);
102: }
103: return null;
104: }
105:
106: /**
107: * Provides <code>JPADataSource</code> interface for <code>Datasource</code>s.
108: */
109: private static class DatasourceWrapper implements Datasource,
110: JPADataSource {
111: private Datasource delegate;
112:
113: DatasourceWrapper(Datasource datasource) {
114: this .delegate = datasource;
115: }
116:
117: public String getJndiName() {
118: return delegate.getJndiName();
119: }
120:
121: public String getUrl() {
122: return delegate.getUrl();
123: }
124:
125: public String getUsername() {
126: return delegate.getUsername();
127: }
128:
129: public String getPassword() {
130: return delegate.getPassword();
131: }
132:
133: public String getDriverClassName() {
134: return delegate.getDriverClassName();
135: }
136:
137: public String getDisplayName() {
138: return delegate.getDisplayName();
139: }
140:
141: @Override
142: public String toString() {
143: return delegate.toString();
144: }
145: }
146: }
|