1 19 20 package org.netbeans.modules.j2ee.persistence.spi.support; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.File ; 25 import java.util.logging.Logger ; 26 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 27 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes; 28 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesFactory; 29 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesImplementation; 30 import org.netbeans.modules.j2ee.persistenceapi.FileChangeSupport; 31 import org.netbeans.modules.j2ee.persistenceapi.FileChangeSupportEvent; 32 import org.netbeans.modules.j2ee.persistenceapi.FileChangeSupportListener; 33 import org.openide.filesystems.FileUtil; 34 35 46 public final class PersistenceScopesHelper { 47 48 private static final Logger LOG = Logger.getLogger(PersistenceScopesHelper.class.getName()); 49 50 private final PersistenceScopes persistenceScopes = PersistenceScopesFactory.createPersistenceScopes(new PersistenceScopesImpl()); 51 private final PropertyChangeSupport propChangeSupport = new PropertyChangeSupport (this); 52 private final FileListener fileListener = new FileListener(); 53 54 private PersistenceScope persistenceScope; 55 private File persistenceXml; 56 private boolean persistenceExists; 57 58 public PersistenceScopesHelper() { 59 } 60 61 73 public void changePersistenceScope(PersistenceScope newPersistenceScope, File newPersistenceXml) { 74 if (newPersistenceScope == null && newPersistenceXml != null) { 75 throw new IllegalArgumentException ("The persistenceScope parameter cannot be null when newPersistenceXml is non-null"); } 77 78 boolean oldPersistenceExists, newPersistenceExists; 79 PersistenceScope oldPersistenceScope; 80 81 synchronized (this) { 82 oldPersistenceExists = persistenceExists; 83 oldPersistenceScope = persistenceScope; 84 85 if (persistenceXml != null) { 86 FileChangeSupport.DEFAULT.removeListener(fileListener, persistenceXml); 87 } 88 if (newPersistenceXml != null) { 89 persistenceXml = newPersistenceXml; 90 FileChangeSupport.DEFAULT.addListener(fileListener, persistenceXml); 91 } else { 92 persistenceXml = null; 93 } 94 95 persistenceScope = newPersistenceScope; 96 persistenceXml = newPersistenceXml; 97 98 change(); 99 100 newPersistenceExists = persistenceExists; 101 } 102 103 if (oldPersistenceExists != newPersistenceExists || (oldPersistenceScope != newPersistenceScope && newPersistenceExists)) { 104 LOG.fine("changePersistenceScope: firing PROP_PERSISTENCE_SCOPES change"); propChangeSupport.firePropertyChange(PersistenceScopes.PROP_PERSISTENCE_SCOPES, null, null); 106 } 107 } 108 109 116 public PersistenceScopes getPersistenceScopes() { 117 return persistenceScopes; 118 } 119 120 124 private void change() { 125 synchronized (this) { 126 persistenceExists = false; 127 if (persistenceXml != null) { 128 persistenceExists = FileUtil.toFileObject(persistenceXml) != null; 129 } 130 } 131 } 132 133 136 private void fileEvent() { 137 boolean oldPersistenceExists, newPersistenceExists; 138 139 synchronized (this) { 140 oldPersistenceExists = persistenceExists; 141 change(); 142 newPersistenceExists = persistenceExists; 143 } 144 145 LOG.fine("fileEvent: oldPersistenceExists=" + oldPersistenceExists + ", newPersistenceExists=" + newPersistenceExists); 147 if (oldPersistenceExists != newPersistenceExists) { 148 LOG.fine("fileEvent: firing PROP_PERSISTENCE_SCOPES change"); propChangeSupport.firePropertyChange(PersistenceScopes.PROP_PERSISTENCE_SCOPES, null, null); 150 } 151 } 152 153 private PersistenceScope[] getPersistenceScopeList() { 154 synchronized (this) { 155 if (persistenceExists) { 156 return new PersistenceScope[] { persistenceScope }; 157 } else { 158 return new PersistenceScope[0]; 159 } 160 } 161 } 162 163 private void addPropertyChangeListener(PropertyChangeListener listener) { 164 propChangeSupport.addPropertyChangeListener(listener); 165 } 166 167 private void removePropertyChangeListener(PropertyChangeListener listener) { 168 propChangeSupport.removePropertyChangeListener(listener); 169 } 170 171 174 private class FileListener implements FileChangeSupportListener { 175 176 public void fileCreated(FileChangeSupportEvent event) { 177 LOG.fine("fileCreated: " + event.getPath()); 178 fileEvent(); 179 } 180 181 public void fileDeleted(FileChangeSupportEvent event) { 182 LOG.fine("fileDeleted: " + event.getPath()); 183 fileEvent(); 184 } 185 186 public void fileModified(FileChangeSupportEvent event) { 187 LOG.fine("fileModified: " + event.getPath()); 188 } 189 } 190 191 196 private class PersistenceScopesImpl implements PersistenceScopesImplementation { 197 198 public void addPropertyChangeListener(PropertyChangeListener listener) { 199 PersistenceScopesHelper.this.addPropertyChangeListener(listener); 200 } 201 202 public void removePropertyChangeListener(PropertyChangeListener listener) { 203 PersistenceScopesHelper.this.removePropertyChangeListener(listener); 204 } 205 206 public PersistenceScope[] getPersistenceScopes() { 207 return PersistenceScopesHelper.this.getPersistenceScopeList(); 208 } 209 } 210 } 211 | Popular Tags |