/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.j2ee.jboss4.config; import java.io.File; import java.io.IOException; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.Set; import org.netbeans.modules.j2ee.deployment.plugins.api.DatasourceManager; import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; import org.netbeans.modules.j2ee.jboss4.config.gen.Datasources; import org.netbeans.modules.j2ee.jboss4.config.gen.LocalTxDatasource; import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties; import org.openide.ErrorManager; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.util.NbBundle; /** * * @author Libor Kotouc */ public final class JBossDatasourceManager implements DatasourceManager { private static final String DSdotXML = "-ds.xml"; // NOI18N // server's deploy dir private FileObject serverDir; public JBossDatasourceManager(String serverUrl) { String serverDirPath = InstanceProperties.getInstanceProperties(serverUrl). getProperty(JBPluginProperties.PROPERTY_DEPLOY_DIR); serverDir = FileUtil.toFileObject(new File(serverDirPath)); } public Set getDatasources() { HashSet datasources = new HashSet(); if (serverDir == null || !serverDir.isValid() || !serverDir.isFolder() || !serverDir.canRead()) { ErrorManager.getDefault().log(ErrorManager.USER, NbBundle.getMessage(JBossDatasourceManager.class, "ERR_WRONG_DEPLOY_DIR")); return datasources; } Enumeration files = serverDir.getChildren(true); LinkedList confs = new LinkedList(); while (files.hasMoreElements()) { // searching for config files with DS FileObject file = (FileObject) files.nextElement(); if (!file.isFolder() && file.getNameExt().endsWith(DSdotXML)) confs.add(file); } if (confs.size() == 0) // nowhere to search return datasources; for (Iterator it = confs.iterator(); it.hasNext();) { try { FileObject dsFO = (FileObject)it.next(); File dsFile = FileUtil.toFile(dsFO); Datasources ds = Datasources.createGraph(dsFile); LocalTxDatasource ltxds[] = ds.getLocalTxDatasource(); for (int i = 0; i < ltxds.length; i++) { if (ltxds[i].getJndiName().length() > 0) { datasources.add(new JBossDatasource( ltxds[i].getJndiName(), ltxds[i].getConnectionUrl(), ltxds[i].getUserName(), ltxds[i].getPassword(), ltxds[i].getDriverClass())); } } } catch (IOException ioe) { ErrorManager.getDefault().notify(ioe); } catch (RuntimeException re) { // -ds.xml is not parseable, do nothing } } return datasources; } }