1 19 20 package org.netbeans.modules.j2ee.jboss4.config; 21 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 import javax.enterprise.deploy.spi.exceptions.ConfigurationException ; 35 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 36 import org.netbeans.modules.j2ee.deployment.common.api.DatasourceAlreadyExistsException; 37 import org.netbeans.modules.j2ee.deployment.plugins.api.DatasourceManager; 38 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 39 import org.netbeans.modules.j2ee.jboss4.config.gen.Datasources; 40 import org.netbeans.modules.j2ee.jboss4.config.gen.LocalTxDatasource; 41 import org.netbeans.modules.j2ee.jboss4.ide.ui.JBPluginProperties; 42 import org.netbeans.modules.schema2beans.BaseBean; 43 import org.openide.ErrorManager; 44 import org.openide.filesystems.FileLock; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileSystem; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.util.NbBundle; 49 50 54 public final class JBossDatasourceManager implements DatasourceManager { 55 56 private static final String DSdotXML = "-ds.xml"; private static final String JBossDSdotXML = "jboss-ds.xml"; 59 private FileObject serverDir; 61 62 public JBossDatasourceManager(String serverUrl) { 63 String serverDirPath = InstanceProperties.getInstanceProperties(serverUrl). 64 getProperty(JBPluginProperties.PROPERTY_DEPLOY_DIR); 65 serverDir = FileUtil.toFileObject(new File (serverDirPath)); 66 } 67 68 public Set <Datasource> getDatasources() { 69 70 Set <Datasource> datasources = new HashSet <Datasource>(); 71 72 if (serverDir == null || !serverDir.isValid() || !serverDir.isFolder() || !serverDir.canRead()) { 73 ErrorManager.getDefault().log(ErrorManager.USER, 74 NbBundle.getMessage(JBossDatasourceManager.class, "ERR_WRONG_DEPLOY_DIR")); 75 return datasources; 76 } 77 78 Enumeration files = serverDir.getChildren(true); 79 List <FileObject> confs = new LinkedList <FileObject>(); 80 while (files.hasMoreElements()) { FileObject file = (FileObject) files.nextElement(); 82 if (!file.isFolder() && file.getNameExt().endsWith(DSdotXML) && file.canRead()) 83 confs.add(file); 84 } 85 86 if (confs.size() == 0) return datasources; 88 89 for (Iterator it = confs.iterator(); it.hasNext();) { 90 try { 91 FileObject dsFO = (FileObject)it.next(); 92 File dsFile = FileUtil.toFile(dsFO); 93 Datasources ds = Datasources.createGraph(dsFile); 94 LocalTxDatasource ltxds[] = ds.getLocalTxDatasource(); 95 for (int i = 0; i < ltxds.length; i++) { 96 if (ltxds[i].getJndiName().length() > 0) { 97 datasources.add(new JBossDatasource( 98 ltxds[i].getJndiName(), 99 ltxds[i].getConnectionUrl(), 100 ltxds[i].getUserName(), 101 ltxds[i].getPassword(), 102 ltxds[i].getDriverClass())); 103 } 104 } 105 } catch (IOException ioe) { 106 ErrorManager.getDefault().notify(ioe); 107 } catch (RuntimeException re) { 108 } 110 } 111 112 return datasources; 113 } 114 115 public void deployDatasources(Set <Datasource> datasources) 116 throws ConfigurationException , DatasourceAlreadyExistsException 117 { 118 Set <Datasource> deployedDS = getDatasources(); 119 Map <String , Datasource> ddsMap = transform(deployedDS); 121 HashMap <String , Datasource> newDS = new HashMap <String , Datasource>(); 123 LinkedList <Datasource> conflictDS = new LinkedList <Datasource>(); 125 for (Iterator <Datasource> it = datasources.iterator(); it.hasNext();) { 126 Object o = it.next(); 127 if (!(o instanceof JBossDatasource)) 128 continue; 129 JBossDatasource ds = (JBossDatasource)o; 130 String jndiName = ds.getJndiName(); 131 if (ddsMap.keySet().contains(jndiName)) { if (!ddsMap.get(jndiName).equals(ds)) { conflictDS.add(ddsMap.get(jndiName)); } 135 } 136 else if (jndiName != null) { 137 newDS.put(jndiName, ds); 138 } 139 } 140 141 if (conflictDS.size() > 0) { throw new DatasourceAlreadyExistsException(conflictDS); 143 } 144 145 FileObject dsXmlFo = serverDir.getFileObject(JBossDSdotXML); 147 File dsXMLFile = (dsXmlFo != null ? FileUtil.toFile(dsXmlFo) : null); 148 149 Datasources deployedDSGraph = null; 150 try { 151 deployedDSGraph = (dsXMLFile != null ? Datasources.createGraph(dsXMLFile) : new Datasources()); 152 } 153 catch (IOException ioe) { 154 ErrorManager.getDefault().annotate(ioe, NbBundle.getMessage(getClass(), "ERR_CannotReadDSdotXml")); 155 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 156 return; 157 } 158 159 LocalTxDatasource ltxds[] = deployedDSGraph.getLocalTxDatasource(); 161 for (int i = 0; i < ltxds.length; i++) { 162 String jndiName = ltxds[i].getJndiName(); 163 if (newDS.keySet().contains(jndiName)) deployedDSGraph.removeLocalTxDatasource(ltxds[i]); 165 } 166 167 for (Iterator it = newDS.values().iterator(); it.hasNext();) { 169 JBossDatasource ds = (JBossDatasource) it.next(); 170 171 LocalTxDatasource lds = new LocalTxDatasource(); 172 lds.setJndiName(ds.getJndiName()); 173 lds.setConnectionUrl(ds.getUrl()); 174 lds.setDriverClass(ds.getDriverClassName()); 175 lds.setUserName(ds.getUsername()); 176 lds.setPassword(ds.getPassword()); 177 lds.setMinPoolSize(ds.getMinPoolSize()); 178 lds.setMaxPoolSize(ds.getMaxPoolSize()); 179 lds.setIdleTimeoutMinutes(ds.getIdleTimeoutMinutes()); 180 181 deployedDSGraph.addLocalTxDatasource(lds); 182 } 183 184 if (newDS.size() > 0) { 186 if (dsXMLFile == null) { 187 try { 188 dsXmlFo = serverDir.createData(JBossDSdotXML); 189 } 190 catch (IOException ioe) { 191 ErrorManager.getDefault().annotate(ioe, NbBundle.getMessage(getClass(), "ERR_CannotCreateDSdotXml")); 192 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 193 return; 194 } 195 196 dsXMLFile = FileUtil.toFile(dsXmlFo); 197 } 198 199 writeFile(dsXMLFile, deployedDSGraph); 200 } 201 202 } 203 204 private Map <String , Datasource> transform(Set <Datasource> datasources) { 205 HashMap <String , Datasource> map = new HashMap <String , Datasource>(); 206 for (Iterator it = datasources.iterator(); it.hasNext();) { 207 JBossDatasource ds = (JBossDatasource) it.next(); 208 if (ds.getJndiName() != null) 209 map.put(ds.getJndiName(), ds); 210 } 211 return map; 212 } 213 214 private void writeFile(final File file, final BaseBean bean) throws ConfigurationException { 215 try { 216 217 FileSystem fs = serverDir.getFileSystem(); 218 fs.runAtomicAction(new FileSystem.AtomicAction() { 219 public void run() throws IOException { 220 OutputStream os = null; 221 FileLock lock = null; 222 try { 223 String name = file.getName(); 224 FileObject configFO = serverDir.getFileObject(name); 225 if (configFO == null) { 226 configFO = serverDir.createData(name); 227 } 228 lock = configFO.lock(); 229 os = new BufferedOutputStream (configFO.getOutputStream(lock), 4096); 230 if (bean != null) { 232 bean.write(os); 233 } 234 } finally { 235 if (os != null) { 236 try { os.close(); } catch(IOException ioe) {} 237 } 238 if (lock != null) 239 lock.releaseLock(); 240 } 241 } 242 }); 243 } catch (IOException e) { 244 throw new ConfigurationException (e.getLocalizedMessage ()); 245 } 246 } 247 248 } 249 | Popular Tags |