KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > spi > support > PersistenceScopesHelper


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.persistence.spi.support;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.logging.Logger JavaDoc;
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 /**
36  * Helper class for implementing
37  * {@link org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesProvider}.
38  * It creates and maintains a {@link org.netbeans.modules.j2ee.persistence.api.PersistenceScopes}
39  * instance containing a single {@link org.netbeans.modules.j2ee.persistence.api.PersistenceScope}
40  * or an empty array of <code>PersistenceScope</code> depending on whether the persistence.xml
41  * file corresponding to that <code>PersistenceScope</code> exists or not, firing property changes
42  * as the persistence.xml file is created/deleted.
43  *
44  * @author Andrei Badea
45  */

46 public final class PersistenceScopesHelper {
47
48     private static final Logger JavaDoc LOG = Logger.getLogger(PersistenceScopesHelper.class.getName());
49
50     private final PersistenceScopes persistenceScopes = PersistenceScopesFactory.createPersistenceScopes(new PersistenceScopesImpl());
51     private final PropertyChangeSupport JavaDoc propChangeSupport = new PropertyChangeSupport JavaDoc(this);
52     private final FileListener fileListener = new FileListener();
53
54     private PersistenceScope persistenceScope;
55     private File JavaDoc persistenceXml;
56     private boolean persistenceExists;
57
58     public PersistenceScopesHelper() {
59     }
60
61     /**
62      * Call this method in order to change the persistence scope returned by the
63      * <code>PersistenceScopes</code> instance returned by {@link #getPersistenceScopes}
64      * or the corresponding persistence.xml file.
65      *
66      * @param newPersistenceScope the new persistence scope; can be null, but in this case
67      * the <code>newPersistenceXml</code> parameter must be null too.
68      * @param newPersistenceXml the new persistence.xml file; can be null.
69      *
70      * @throws IllegalArgumentException if <code>newPersistenceScope</code> is null
71      * and <code>newPersistenceXml</code> is not.
72      */

73     public void changePersistenceScope(PersistenceScope newPersistenceScope, File JavaDoc newPersistenceXml) {
74         if (newPersistenceScope == null && newPersistenceXml != null) {
75             throw new IllegalArgumentException JavaDoc("The persistenceScope parameter cannot be null when newPersistenceXml is non-null"); // NOI18N
76
}
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"); // NOI18N
105
propChangeSupport.firePropertyChange(PersistenceScopes.PROP_PERSISTENCE_SCOPES, null, null);
106         }
107     }
108
109     /**
110      * Returns the <code>PersistenceScopes</code> created by this helper. Usually
111      * an implementor of <code>PersistenceScopesProvider</code> will delegate
112      * its <code>getPersistenceScopes</code> method to this method.
113      *
114      * @return a <code>PersistenceScopes</code> instance; never null.
115      */

116     public PersistenceScopes getPersistenceScopes() {
117         return persistenceScopes;
118     }
119
120     /**
121      * Called when anything has changed (the persistence scope has changed, the path of the
122      * persistence.xml file has changes, the persistence.xml file has been created or deleted).
123      */

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     /**
134      * Called when something happened to persistence.xml (created, deleted).
135      */

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); // NOI18N
146

147         if (oldPersistenceExists != newPersistenceExists) {
148             LOG.fine("fileEvent: firing PROP_PERSISTENCE_SCOPES change"); // NOI18N
149
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 JavaDoc listener) {
164         propChangeSupport.addPropertyChangeListener(listener);
165     }
166
167     private void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
168         propChangeSupport.removePropertyChangeListener(listener);
169     }
170
171     /**
172      * Listener on the persistence.xml file.
173      */

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     /**
192      * Implementation of <code>PersistenceScopesImplementation</code>.
193      * The <code>PersistenceScopes</code> instance maintained by the helper
194      * delegates to this.
195      */

196     private class PersistenceScopesImpl implements PersistenceScopesImplementation {
197
198         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
199             PersistenceScopesHelper.this.addPropertyChangeListener(listener);
200         }
201
202         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
203             PersistenceScopesHelper.this.removePropertyChangeListener(listener);
204         }
205
206         public PersistenceScope[] getPersistenceScopes() {
207             return PersistenceScopesHelper.this.getPersistenceScopeList();
208         }
209     }
210 }
211
Popular Tags