1 19 package org.netbeans.modules.j2ee.sun.ide.sunresources.resourcesloader; 20 21 import java.io.InputStream ; 22 import org.xml.sax.InputSource ; 23 24 25 import org.openide.loaders.DataObjectExistsException; 26 import org.openide.loaders.XMLDataObject; 27 import org.openide.nodes.Node; 28 import org.openide.util.HelpCtx; 29 import org.openide.util.NbBundle; 30 import org.openide.ErrorManager; 31 32 import org.netbeans.api.xml.cookies.CheckXMLCookie; 33 import org.netbeans.api.xml.cookies.ValidateXMLCookie; 34 import org.netbeans.spi.xml.cookies.CheckXMLSupport; 35 import org.netbeans.spi.xml.cookies.DataObjectAdapters; 36 import org.netbeans.spi.xml.cookies.ValidateXMLSupport; 37 38 import org.netbeans.modules.j2ee.sun.dd.api.DDProvider; 39 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JdbcConnectionPool; 40 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.JdbcResource; 41 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.AdminObjectResource; 42 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.ConnectorResource; 43 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.ConnectorConnectionPool; 44 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.MailResource; 45 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.PersistenceManagerFactoryResource; 46 import org.netbeans.modules.j2ee.sun.dd.api.serverresources.Resources; 47 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ConnPoolBean; 48 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.ConnPoolBeanDataNode; 49 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.DataSourceBean; 50 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.DataSourceBeanDataNode; 51 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JMSBean; 52 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JMSBeanDataNode; 53 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JavaMailSessionBean; 54 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.JavaMailSessionBeanDataNode; 55 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.PersistenceManagerBean; 56 import org.netbeans.modules.j2ee.sun.ide.sunresources.beans.PersistenceManagerBeanDataNode; 57 58 import org.openide.filesystems.FileAttributeEvent; 59 import org.openide.filesystems.FileChangeListener; 60 import org.openide.filesystems.FileEvent; 61 import org.openide.filesystems.FileObject; 62 import org.openide.filesystems.FileRenameEvent; 63 import org.openide.util.WeakListeners; 64 65 69 public class SunResourceDataObject extends XMLDataObject implements FileChangeListener { private static String JDBC_CP = "jdbc-connection-pool"; private static String JDBC_DS = "jdbc-resource"; private static String PMF = "persistence-manager-factory-resource"; private static String MAIL = "mail-resource"; private static String JMS = "jms-resource"; 76 private ValidateXMLCookie validateCookie = null; 77 private CheckXMLCookie checkCookie = null; 78 79 ConnPoolBean cpBean = null; 80 DataSourceBean dsBean = null; 81 PersistenceManagerBean pmfBean = null; 82 JavaMailSessionBean mailBean = null; 83 JMSBean jmsBean = null; 84 85 String resType; 86 87 public SunResourceDataObject(FileObject pf, SunResourceDataLoader loader) throws DataObjectExistsException { 88 super(pf, loader); 89 pf.addFileChangeListener((FileChangeListener) WeakListeners.create(FileChangeListener.class, this, pf)); 90 91 resType = getResource(pf); 92 } 94 95 111 113 public org.openide.nodes.Node.Cookie getCookie(Class c) { 114 Node.Cookie retValue = null; 115 if (ValidateXMLCookie.class.isAssignableFrom(c)) { 116 if (validateCookie == null) { 117 InputSource in = DataObjectAdapters.inputSource(this); 118 validateCookie = new ValidateXMLSupport(in); 119 } 120 return validateCookie; 121 } else if (CheckXMLCookie.class.isAssignableFrom(c)) { 122 if (checkCookie == null) { 123 InputSource in = DataObjectAdapters.inputSource(this); 124 checkCookie = new CheckXMLSupport(in); 125 } 126 return checkCookie; 127 } 128 129 if (retValue == null) { 130 retValue = super.getCookie(c); 131 } 132 return retValue; 133 } 134 135 136 public HelpCtx getHelpCtx() { 137 return null; } 141 142 protected Node createNodeDelegate() { 143 if(resType != null){ 144 if(this.resType.equals(this.JDBC_CP)){ 145 Node node = new ConnPoolBeanDataNode(this, getPool()); 146 return node; 147 }if(this.resType.equals(this.JDBC_DS)){ 148 Node node = new DataSourceBeanDataNode(this, getDataSource()); 149 return node; 150 }if(this.resType.equals(this.PMF)){ 151 Node node = new PersistenceManagerBeanDataNode(this, getPersistenceManager()); 152 return node; 153 }if(this.resType.equals(this.MAIL)){ 154 Node node = new JavaMailSessionBeanDataNode(this, getMailSession()); 155 return node; 156 }if(this.resType.equals(this.JMS)){ 157 Node node = new JMSBeanDataNode(this, getJMS()); 158 return node; 159 }else{ 160 String mess = NbBundle.getMessage(SunResourceDataObject.class, "Info_notSunResource"); ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, mess); 162 return new SunResourceDataNode(this); 163 } 164 }else{ 165 return new SunResourceDataNode(this); 166 } 167 } 168 169 private String getResource(FileObject primaryFile) { 170 String type = null; 171 try { 172 if((! primaryFile.isFolder()) && primaryFile.isValid()){ 173 InputStream in = primaryFile.getInputStream(); 174 Resources resources = DDProvider.getDefault().getResourcesGraph(in); 175 176 JdbcConnectionPool[] pools = resources.getJdbcConnectionPool(); 178 if(pools.length != 0){ 179 ConnPoolBean currCPBean = ConnPoolBean.createBean(pools[0]); 180 type = this.JDBC_CP; 181 setPool(currCPBean); 182 return type; 183 } 184 185 JdbcResource[] dataSources = resources.getJdbcResource(); 187 if(dataSources.length != 0){ 188 DataSourceBean currDSBean = DataSourceBean.createBean(dataSources[0]); 189 type = this.JDBC_DS; 190 setDataSource(currDSBean); 191 return type; 192 } 193 194 PersistenceManagerFactoryResource[] pmfResources = resources.getPersistenceManagerFactoryResource(); 196 if(pmfResources.length != 0){ 197 PersistenceManagerBean currPMFBean = PersistenceManagerBean.createBean(pmfResources[0]); 198 type = this.PMF; 199 setPersistenceManager(currPMFBean); 200 return type; 201 } 202 203 MailResource[] mailResources = resources.getMailResource(); 205 if(mailResources.length != 0){ 206 JavaMailSessionBean currMailBean = JavaMailSessionBean.createBean(mailResources[0]); 207 type = this.MAIL; 208 setMailSession(currMailBean); 209 return type; 210 } 211 212 AdminObjectResource[] aoResources = resources.getAdminObjectResource(); 214 if(aoResources.length != 0){ 215 JMSBean jmsBean = JMSBean.createBean(aoResources[0]); 216 type = this.JMS; 217 setJMS(jmsBean); 218 return type; 219 } 220 221 ConnectorResource[] connResources = resources.getConnectorResource(); 222 ConnectorConnectionPool[] connPoolResources = resources.getConnectorConnectionPool(); 223 if(connResources.length != 0 && connPoolResources.length != 0){ 224 JMSBean jmsBean = JMSBean.createBean(resources); 225 type = this.JMS; 226 setJMS(jmsBean); 227 return type; 228 } 229 230 return type; 231 }else 232 return type; 233 }catch(NullPointerException npe){ 234 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, npe); 235 return type; 236 }catch(Exception ex){ 237 return type; 239 } 240 241 } 242 243 private void setPool(ConnPoolBean in_cpBean){ 244 this.cpBean = in_cpBean; 245 } 246 247 private ConnPoolBean getPool(){ 248 return this.cpBean; 249 } 250 251 private void setDataSource(DataSourceBean in_dsBean){ 252 this.dsBean = in_dsBean; 253 } 254 255 private DataSourceBean getDataSource(){ 256 return this.dsBean; 257 } 258 259 private void setPersistenceManager(PersistenceManagerBean in_pmfBean){ 260 this.pmfBean = in_pmfBean; 261 } 262 263 private PersistenceManagerBean getPersistenceManager(){ 264 return this.pmfBean; 265 } 266 267 private void setMailSession(JavaMailSessionBean in_mailBean){ 268 this.mailBean = in_mailBean; 269 } 270 271 private JavaMailSessionBean getMailSession(){ 272 return this.mailBean; 273 } 274 275 private void setJMS(JMSBean in_jmsBean){ 276 this.jmsBean = in_jmsBean; 277 } 278 279 private JMSBean getJMS(){ 280 return this.jmsBean; 281 } 282 283 public void fileAttributeChanged (FileAttributeEvent fe) { 284 updateDataObject(); 285 } 286 287 public void fileChanged (FileEvent fe) { 288 updateDataObject(); 289 } 290 291 public void fileDataCreated (FileEvent fe) { 292 updateDataObject (); 293 } 294 295 public void fileDeleted (FileEvent fe) { 296 updateDataObject (); 297 } 298 299 public void fileFolderCreated (FileEvent fe) { 300 updateDataObject (); 301 } 302 303 public void fileRenamed (FileRenameEvent fe) { 304 updateDataObject (); 305 } 306 307 private void updateDataObject(){ 308 resType = getResource(this.getPrimaryFile()); 309 } 310 311 public String getResourceType(){ 312 return resType; 313 } 314 316 323 324 } 325 | Popular Tags |