KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > config > JBossDatasourceManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.jboss4.config;
21
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
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 /**
51  *
52  * @author Libor Kotouc
53  */

54 public final class JBossDatasourceManager implements DatasourceManager {
55     
56     private static final String JavaDoc DSdotXML = "-ds.xml"; // NOI18N
57
private static final String JavaDoc JBossDSdotXML = "jboss-ds.xml"; // NOI18N
58

59     // server's deploy dir
60
private FileObject serverDir;
61     
62     public JBossDatasourceManager(String JavaDoc serverUrl) {
63         String JavaDoc serverDirPath = InstanceProperties.getInstanceProperties(serverUrl).
64                                         getProperty(JBPluginProperties.PROPERTY_DEPLOY_DIR);
65         serverDir = FileUtil.toFileObject(new File JavaDoc(serverDirPath));
66     }
67     
68     public Set JavaDoc<Datasource> getDatasources() {
69         
70         Set JavaDoc<Datasource> datasources = new HashSet JavaDoc<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 JavaDoc files = serverDir.getChildren(true);
79         List JavaDoc<FileObject> confs = new LinkedList JavaDoc<FileObject>();
80         while (files.hasMoreElements()) { // searching for config files with DS
81
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) // nowhere to search
87
return datasources;
88
89         for (Iterator JavaDoc it = confs.iterator(); it.hasNext();) {
90             try {
91                 FileObject dsFO = (FileObject)it.next();
92                 File JavaDoc 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 JavaDoc ioe) {
106                 ErrorManager.getDefault().notify(ioe);
107             } catch (RuntimeException JavaDoc re) {
108                 // -ds.xml is not parseable, do nothing
109
}
110         }
111         
112         return datasources;
113     }
114
115     public void deployDatasources(Set JavaDoc<Datasource> datasources)
116     throws ConfigurationException JavaDoc, DatasourceAlreadyExistsException
117     {
118         Set JavaDoc<Datasource> deployedDS = getDatasources();
119         Map JavaDoc<String JavaDoc, Datasource> ddsMap = transform(deployedDS); // for faster searching
120

121         HashMap JavaDoc<String JavaDoc, Datasource> newDS = new HashMap JavaDoc<String JavaDoc, Datasource>(); // will contain all ds which do not conflict with existing ones
122

123         //resolve all conflicts
124
LinkedList JavaDoc<Datasource> conflictDS = new LinkedList JavaDoc<Datasource>();
125         for (Iterator JavaDoc<Datasource> it = datasources.iterator(); it.hasNext();) {
126             Object JavaDoc o = it.next();
127             if (!(o instanceof JBossDatasource))
128                 continue;
129             JBossDatasource ds = (JBossDatasource)o;
130             String JavaDoc jndiName = ds.getJndiName();
131             if (ddsMap.keySet().contains(jndiName)) { // conflicting ds found
132
if (!ddsMap.get(jndiName).equals(ds)) { // found ds is not equal
133
conflictDS.add(ddsMap.get(jndiName)); // NOI18N
134
}
135             }
136             else if (jndiName != null) {
137                 newDS.put(jndiName, ds);
138             }
139         }
140         
141         if (conflictDS.size() > 0) { // conflict found -> exception
142
throw new DatasourceAlreadyExistsException(conflictDS);
143         }
144         
145         //write jboss-ds.xml
146
FileObject dsXmlFo = serverDir.getFileObject(JBossDSdotXML);
147         File JavaDoc 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 JavaDoc ioe) {
154             ErrorManager.getDefault().annotate(ioe, NbBundle.getMessage(getClass(), "ERR_CannotReadDSdotXml"));
155             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
156             return;
157         }
158
159         //merge ds graph with newDS - remove conflicting ds from graph
160
LocalTxDatasource ltxds[] = deployedDSGraph.getLocalTxDatasource();
161         for (int i = 0; i < ltxds.length; i++) {
162             String JavaDoc jndiName = ltxds[i].getJndiName();
163             if (newDS.keySet().contains(jndiName)) //conflict, we must remove it from graph
164
deployedDSGraph.removeLocalTxDatasource(ltxds[i]);
165         }
166         
167         //add all ds from newDS
168
for (Iterator JavaDoc 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         //write modified graph into jboss-ds.xml
185
if (newDS.size() > 0) {
186             if (dsXMLFile == null) {
187                 try {
188                     dsXmlFo = serverDir.createData(JBossDSdotXML);
189                 }
190                 catch (IOException JavaDoc 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 JavaDoc<String JavaDoc, Datasource> transform(Set JavaDoc<Datasource> datasources) {
205         HashMap JavaDoc<String JavaDoc, Datasource> map = new HashMap JavaDoc<String JavaDoc, Datasource>();
206         for (Iterator JavaDoc 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 JavaDoc file, final BaseBean bean) throws ConfigurationException JavaDoc {
215         try {
216
217             FileSystem fs = serverDir.getFileSystem();
218             fs.runAtomicAction(new FileSystem.AtomicAction() {
219                 public void run() throws IOException JavaDoc {
220                     OutputStream JavaDoc os = null;
221                     FileLock lock = null;
222                     try {
223                         String JavaDoc 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 JavaDoc (configFO.getOutputStream(lock), 4096);
230                         // TODO notification needed
231
if (bean != null) {
232                             bean.write(os);
233                         }
234                     } finally {
235                         if (os != null) {
236                             try { os.close(); } catch(IOException JavaDoc ioe) {}
237                         }
238                         if (lock != null)
239                             lock.releaseLock();
240                     }
241                 }
242             });
243         } catch (IOException JavaDoc e) {
244             throw new ConfigurationException JavaDoc (e.getLocalizedMessage ());
245         }
246     }
247     
248 }
249
Popular Tags