KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > provider > ProviderUtil


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.provider;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.netbeans.api.db.explorer.ConnectionManager;
26 import org.netbeans.api.db.explorer.DatabaseConnection;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.modules.j2ee.persistence.api.PersistenceLocation;
29 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
30 import org.netbeans.modules.j2ee.persistence.dd.PersistenceUtils;
31 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
32 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Properties;
33 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Property;
34 import org.netbeans.modules.j2ee.persistence.spi.provider.PersistenceProviderSupplier;
35 import org.netbeans.modules.j2ee.persistence.spi.server.ServerStatusProvider;
36 import org.netbeans.modules.j2ee.persistence.unit.*;
37 import org.netbeans.modules.j2ee.persistence.wizard.Util;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileLock;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileSystem;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.filesystems.Repository;
44 import org.openide.loaders.DataObject;
45 import org.openide.loaders.DataObjectNotFoundException;
46 import org.openide.util.Parameters;
47
48 /**
49  * A utility class for handling persistence units and providers. Provides means
50  * for constructing a persistence unit and for getting/setting/changing
51  * properties of persistence units.
52  *
53  * @author Martin Adamek, Erno Mononen
54  */

55 public class ProviderUtil {
56     
57     // known providers
58
public static final Provider HIBERNATE_PROVIDER = new HibernateProvider();
59     public static final Provider TOPLINK_PROVIDER = new ToplinkProvider();
60     public static final Provider KODO_PROVIDER = new KodoProvider();
61     public static final Provider DEFAULT_PROVIDER = new DefaultProvider();
62     
63     private ProviderUtil() {
64     }
65     
66     /**
67      * Gets the persistence provider identified by the given <code>providerClass</code>.
68      * If the given class was empty or null, will return the default persistence provider
69      * of the given project's target server, or null if a default provider is not supported
70      * in the given project.
71      *
72      * @param providerClass the FQN of the class that specifies the persistence provider.
73      *
74      * @return the provider that the given providerClass represents or null if it was
75      * an empty string and the project doesn't suppport a default (container managed)
76      * persistence provider.
77      */

78     public static Provider getProvider(String JavaDoc providerClass, Project project){
79         
80         if (null == providerClass || "".equals(providerClass.trim())){
81             return getContainerManagedProvider(project);
82         }
83         
84         for (Provider each : getAllProviders()){
85             if (each.getProviderClass().equals(providerClass.trim())){
86                 return each;
87             }
88         }
89         // some unknown provider
90
return DEFAULT_PROVIDER;
91         
92     }
93     
94     
95     /*
96      * Gets the default persistence provider of the target server
97      * of the given <code>project</code>.
98      *
99      * @return the default container managed provider for the given project or <code>null</code>
100      * no default provider could be resolved.
101      *
102      * @throws NullPointerException if the given project was null.
103      */

104     private static Provider getContainerManagedProvider(Project project){
105         
106         PersistenceProviderSupplier providerSupplier = project.getLookup().lookup(PersistenceProviderSupplier.class);
107         
108         if (providerSupplier == null
109                 || !providerSupplier.supportsDefaultProvider()
110                 || providerSupplier.getSupportedProviders().isEmpty()){
111             
112             return null;
113         }
114         
115         return providerSupplier.getSupportedProviders().get(0);
116     }
117     
118     /**
119      * Gets the database connection specified in the given persistence
120      * unit.
121      *
122      * @param pu the persistence unit whose database connection is to
123      * be retrieved; must not be null.
124      *
125      * @rerturn the connection specified in the given persistence unit or
126      * <code>null</code> if it didn't specify a connectioh.
127      *
128      */

129     public static DatabaseConnection getConnection(PersistenceUnit pu) {
130         
131         Parameters.notNull("pu", pu); //NO18N
132

133         if (pu.getProperties() == null){
134             return null;
135         }
136         
137         String JavaDoc url = null;
138         String JavaDoc driver = null;
139         String JavaDoc username = null;
140         Property[] properties = pu.getProperties().getProperty2();
141         Provider provider = getProvider(pu);
142         
143         for (int i = 0; i < properties.length; i++) {
144             String JavaDoc key = properties[i].getName();
145             if (key == null){
146                 continue;
147             }
148             if (key.equals(provider.getJdbcUrl())) {
149                 url = properties[i].getValue();
150             } else if (key.equals(provider.getJdbcDriver())) {
151                 driver = properties[i].getValue();
152             } else if (key.equals(provider.getJdbcUsername())) {
153                 username = properties[i].getValue();
154             }
155         }
156         DatabaseConnection[] connections = ConnectionManager.getDefault().getConnections();
157         
158         for (int i = 0; i < connections.length; i++) {
159             DatabaseConnection c = connections[i];
160             // password is problematic, when it is returned?
161
if (c.getDatabaseURL().equals(url) &&
162                     c.getDriverClass().equals(driver) &&
163                     c.getUser().equals(username)) {
164                 return c;
165             }
166         }
167         return null;
168     }
169     
170     /**
171      * Sets the given table generation strategy for given persistence unit.
172      * @param persistenceUnit
173      * @param tableGenerationStrategy the strategy to set, see constants in <code>Provider</code>
174      * @project the project of the given persistence unit
175      */

176     public static void setTableGeneration(PersistenceUnit persistenceUnit, String JavaDoc tableGenerationStrategy, Project project) {
177         String JavaDoc providerClass = persistenceUnit.getProvider();
178         Provider provider = ProviderUtil.getProvider(providerClass, project);
179         setTableGeneration(persistenceUnit, tableGenerationStrategy, provider);
180     }
181     
182     /**
183      * Sets the given table generation strategy for the given persistence unit.
184      *
185      * @param persistenceUnit the persistenceUnit to which the given strategy is to be set.
186      * @param tableGenerationStrategy the strategy to set, see constants in <code>Provider</code> for
187      * options.
188      * @provider the provider whose table generation property will be used.
189      */

190     public static void setTableGeneration(PersistenceUnit persistenceUnit, String JavaDoc tableGenerationStrategy, Provider provider){
191         Property tableGenerationProperty = provider.getTableGenerationProperty(tableGenerationStrategy);
192         Properties properties = persistenceUnit.getProperties();
193         if (properties == null) {
194             properties = persistenceUnit.newProperties();
195             persistenceUnit.setProperties(properties);
196         }
197         
198         Property existing = getProperty(properties.getProperty2(), provider.getTableGenerationPropertyName());
199         
200         if (existing != null && tableGenerationProperty == null){
201             properties.removeProperty2(existing);
202         } else if (existing != null && tableGenerationProperty != null){
203             existing.setValue(tableGenerationProperty.getValue());
204         } else if (tableGenerationProperty != null){
205             properties.addProperty2(tableGenerationProperty);
206         }
207         
208     }
209     
210     /**
211      * Sets the given provider, connection and table generation strategy to the given persistence unit. Note
212      * that if the given persistence unit already had an existing provider, its existing properties are not preserved
213      * with the exception of the database connection properties. In other words, you have to explicitly set for
214      * example a table generation strategy for the persistence unit after changing the provider.
215      *
216      * @param persistenceUnit the persistence unit to which the other params are to be set; must not be null.
217      * @param provider the provider to set; must not be null.
218      * @connection the connection to set; must not be null.
219      * @tableGenerationStrategy the table generation strategy to set.
220      */

221     public static void setProvider(PersistenceUnit persistenceUnit, Provider provider,
222             DatabaseConnection connection, String JavaDoc tableGenerationStrategy){
223         
224         Parameters.notNull("persistenceUnit", persistenceUnit); //NO18N
225
Parameters.notNull("connection", connection); //NO18N
226
Parameters.notNull("provider", provider); //NO18N
227

228         removeProviderProperties(persistenceUnit);
229         persistenceUnit.setProvider(provider.getProviderClass());
230         setDatabaseConnection(persistenceUnit, connection);
231         setTableGeneration(persistenceUnit, tableGenerationStrategy, provider);
232     }
233     
234     
235     /**
236      * Removes all provider specific properties from the given persistence unit.
237      * Should be called before setting a new provider for persistence units.
238      *
239      * @param persistenceUnit the persistence unit whose provider specific
240      * properties are to be removed; must not be null.
241      */

242     public static void removeProviderProperties(PersistenceUnit persistenceUnit){
243         Parameters.notNull("persistenceUnit", persistenceUnit); //NO18N
244

245         Provider old = getProvider(persistenceUnit);
246         Property[] properties = getProperties(persistenceUnit);
247         
248         if (old != null){
249             for (int i = 0; i < properties.length; i++) {
250                 Property each = properties[i];
251                 if (old.getPropertyNames().contains(each.getName())){
252                     persistenceUnit.getProperties().removeProperty2(each);
253                 }
254             }
255         }
256         persistenceUnit.setProvider(null);
257         
258     }
259     
260     
261     /**
262      * Constructs a persistence unit based on the given paramaters. Takes care of
263      * setting the default vendor specific properties (if any) to the created
264      * persistence unit.
265      *
266      * @param name the name for the persistence unit; must not be null.
267      * @param provider the provider for the persitence unit; must not be null.
268      * @param connection the database connection for the persistence unit; must not be null.
269      *
270      * @return the created persistence unit.
271      */

272     public static PersistenceUnit buildPersistenceUnit(String JavaDoc name, Provider provider, DatabaseConnection connection) {
273         
274         Parameters.notNull("name", name);
275         Parameters.notNull("provider", provider);
276         Parameters.notNull("connection", connection);
277         
278         PersistenceUnit persistenceUnit = new PersistenceUnit();
279         persistenceUnit.setName(name);
280         persistenceUnit.setProvider(provider.getProviderClass());
281         Properties properties = persistenceUnit.newProperties();
282         Map JavaDoc connectionProperties = provider.getConnectionPropertiesMap(connection);
283         for (Iterator JavaDoc it = connectionProperties.keySet().iterator(); it.hasNext();) {
284             String JavaDoc propertyName = (String JavaDoc) it.next();
285             Property property = properties.newProperty();
286             property.setName(propertyName);
287             property.setValue((String JavaDoc) connectionProperties.get(propertyName));
288             properties.addProperty2(property);
289         }
290         
291         Map JavaDoc defaultProperties = provider.getDefaultVendorSpecificProperties();
292         for (Iterator JavaDoc it = defaultProperties.keySet().iterator(); it.hasNext();) {
293             String JavaDoc propertyName = (String JavaDoc) it.next();
294             Property property = properties.newProperty();
295             property.setName(propertyName);
296             property.setValue((String JavaDoc) defaultProperties.get(propertyName));
297             properties.addProperty2(property);
298         }
299         
300         persistenceUnit.setProperties(properties);
301         return persistenceUnit;
302     }
303     
304     
305     /**
306      * Sets the properties of the given connection to the given persistence unit.
307      *
308      * @param persistenceUnit the persistence unit to which the connection properties
309      * are to be set. Must not be null.
310      * @param connection the database connections whose properties are to be set. Must
311      * not be null.
312      */

313     public static void setDatabaseConnection(PersistenceUnit persistenceUnit, DatabaseConnection connection){
314         
315         Parameters.notNull("persistenceUnit", persistenceUnit); //NO18N
316
Parameters.notNull("connection", connection); //NO18N
317

318         Provider provider = getProvider(persistenceUnit);
319         Property[] properties = getProperties(persistenceUnit);
320         
321         Map JavaDoc<String JavaDoc, String JavaDoc> propertiesMap = provider.getConnectionPropertiesMap(connection);
322         
323         for (String JavaDoc name : propertiesMap.keySet()) {
324             Property property = getProperty(properties, name);
325             if (property == null){
326                 
327                 if (persistenceUnit.getProperties() == null){
328                     persistenceUnit.setProperties(persistenceUnit.newProperties());
329                 }
330                 
331                 property = persistenceUnit.getProperties().newProperty();
332                 property.setName(name);
333                 persistenceUnit.getProperties().addProperty2(property);
334             }
335             
336             String JavaDoc value = propertiesMap.get(name);
337             // value must be present (setting null would cause
338
// value attribute to not be present)
339
if (value == null){
340                 value = "";
341             }
342             property.setValue(value);
343         }
344     }
345     
346     /**
347      * Gets the properties of the given persistence unit. If the properties of
348      * given unit were null, will return an empty array.
349      *
350      * @return array of properties, empty if the given unit's properties were null.
351      */

352     static Property[] getProperties(PersistenceUnit persistenceUnit){
353         if (persistenceUnit.getProperties() != null){
354             return persistenceUnit.getProperties().getProperty2();
355         }
356         return new Property[0];
357     }
358     
359     /**
360      * @return the property from the given properties whose name matches
361      * the given propertyName
362      * or null if the given properties didn't contain property with a matching name.
363      */

364     private static Property getProperty(Property[] properties, String JavaDoc propertyName){
365         
366         if (null == properties){
367             return null;
368         }
369         
370         for (int i = 0; i < properties.length; i++) {
371             Property each = properties[i];
372             if (each.getName() != null && each.getName().equals(propertyName)){
373                 return each;
374             }
375         }
376         
377         return null;
378     }
379     
380     /**
381      * Gets the property that matches the given <code>propertyName</code> from the
382      * given <code>persistenceUnit</code>.
383      *
384      * @return the matching property or null if the given persistence unit didn't
385      * contain a property with a matching name.
386      */

387     public static Property getProperty(PersistenceUnit persistenceUnit, String JavaDoc propertyName){
388         if (persistenceUnit.getProperties() == null){
389             return null;
390         }
391         return getProperty(persistenceUnit.getProperties().getProperty2(), propertyName);
392     }
393     
394     /**
395      * Gets the persistence provider of the given persistence unit.
396      *
397      * @param persistenceUnit the persistence unit whose provider is to
398      * be get. Must not be null.
399      *
400      * @return the provider of the given persistence unit. In case that no specific
401      * provider can be resolved <code>DEFAULT_PROVIDER</code> will be returned.
402      */

403     public static Provider getProvider(PersistenceUnit persistenceUnit){
404         Parameters.notNull("persistenceUnit", persistenceUnit); //NO18N
405

406         for (Provider each : getAllProviders()){
407             if(each.getProviderClass().equals(persistenceUnit.getProvider())){
408                 return each;
409             }
410         }
411         return DEFAULT_PROVIDER;
412     }
413     
414     /**
415      *@return true if the given puDataObject is not null and its document is
416      * parseable, false otherwise.
417      */

418     public static boolean isValid(PUDataObject puDataObject){
419         return null == puDataObject ? false : puDataObject.parseDocument();
420     }
421     
422     /**
423      * Gets the persistence units that are defined in the given <code>
424      * puDataObject</code>.
425      *
426      * @param puDataObject the PUDataObject whose persistence units are to be retrived.
427      *
428      * @return the persistence units specified in the given <code>puDataObject</code>
429      * or an empty array if there were no persistence units defined in it.
430      */

431     public static PersistenceUnit[] getPersistenceUnits(PUDataObject puDataObject){
432         if (puDataObject.getPersistence() == null){
433             return new PersistenceUnit[0];
434         }
435         return puDataObject.getPersistence().getPersistenceUnit();
436     }
437     
438     /**
439      * Renames given managed class in given persistence unit.
440      * @param persistenceUnit the unit that contains the class to be renamed.
441      * @param newName the new name of the class.
442      * @param oldName the name of the class to be renamed.
443      * @param dataObject
444      *
445      */

446     public static void renameManagedClass(PersistenceUnit persistenceUnit, String JavaDoc newName,
447             String JavaDoc oldName, PUDataObject dataObject){
448         
449         dataObject.removeClass(persistenceUnit, oldName);
450         dataObject.addClass(persistenceUnit, newName);
451         
452     }
453     
454     /**
455      * Removes given managed class from given persistence unit.
456      * @param persistenceUnit the persistence unit from which the given class
457      * is to be removed.
458      * @param clazz fully qualified name of the class to be removed.
459      * @param dataObject the data object representing persistence.xml.
460      */

461     public static void removeManagedClass(PersistenceUnit persistenceUnit, String JavaDoc clazz,
462             PUDataObject dataObject){
463         
464         dataObject.removeClass(persistenceUnit, clazz);
465     }
466     
467     /**
468      * Adds given managed class to given persistence unit.
469      * @param persistenceUnit the persistence unit to which the given class
470      * is to be added.
471      * @param clazz fully qualified name of the class to be added.
472      * @param dataObject the data object representing persistence.xml.
473      */

474     public static void addManagedClass(PersistenceUnit persistenceUnit, String JavaDoc clazz,
475             PUDataObject dataObject){
476         
477         dataObject.addClass(persistenceUnit, clazz);
478     }
479     
480     
481     /**
482      * Adds the given <code>persistenceUnit</code> to the <code>PUDataObject<code>
483      * of the given <code>project</code> and saves it.
484      * @param persistenceUnit the unit to be added
485      * @param project the project to which the unit is to be added.
486      * @throws InvalidPersistenceXmlException if the given project has an invalid persistence.xml file.
487      *
488      */

489     public static void addPersistenceUnit(PersistenceUnit persistenceUnit, Project project) throws InvalidPersistenceXmlException{
490         PUDataObject pud = getPUDataObject(project);
491         pud.addPersistenceUnit(persistenceUnit);
492         pud.save();
493     }
494     
495     /**
496      *Gets the <code>PUDataObject</code> associated with the given <code>fo</code>.
497      *
498      *@param fo the file object thas has an associated <code>PUDataObject</code>. Must
499      * not be null.
500      *
501      *@return the <code>PUDataObject</code> associated with the given <code>fo</code>.
502      *
503      *@throws IllegalArgumentException if the given <code>fo</code> is null.
504      *@throws InvalidPersistenceXmlException if the given file object represents
505      * an invalid persistence.xml file.
506      */

507     public static PUDataObject getPUDataObject(FileObject fo) throws InvalidPersistenceXmlException{
508         Parameters.notNull("fo", fo); //NO18N
509

510         DataObject dataObject = null;
511         try {
512             dataObject = DataObject.find(fo);
513         } catch (DataObjectNotFoundException ex) {
514             ErrorManager.getDefault().notify(ex);
515         }
516         if (!(dataObject instanceof PUDataObject)){
517             throw new InvalidPersistenceXmlException(FileUtil.getFileDisplayName(fo));
518         }
519         return (PUDataObject) dataObject;
520     }
521     
522     /**
523      * Gets the PUDataObject associated with the given <code>project</code>. If there
524      * was no PUDataObject (i.e. no persistence.xml) in the project, a new one
525      * will be created. Use
526      * {@link #getDDFile} for testing whether a project has a persistence.xml file.
527      *
528      *@param project the project whose PUDataObject is to be get. Must not be null.
529      *
530      *@return <code>PUDataObject</code> associated with the given project; never null.
531      *
532      * @throws InvalidPersistenceXmlException if the given <code>project</code> had an existing
533      * invalid persitence.xml file.
534      */

535     public static PUDataObject getPUDataObject(Project project) throws InvalidPersistenceXmlException{
536         Parameters.notNull("project", project); //NO18N
537

538         FileObject puFileObject = getDDFile(project);
539         if (puFileObject == null) {
540             puFileObject = createPersistenceDDFile(project);
541         }
542         return getPUDataObject(puFileObject);
543     }
544     
545     /**
546      * Creates a new FileObject representing file that defines
547      * persistence units (<tt>persistence.xml</tt>). <i>Todo: move somewhere else?</i>
548      * @return FileObject representing <tt>persistence.xml</tt>.
549      */

550     private static FileObject createPersistenceDDFile(Project project){
551         final FileObject[] dd = new FileObject[1];
552         try {
553             final FileObject persistenceLocation = PersistenceLocation.createLocation(project);
554             // must create the file using AtomicAction, see #72058
555
persistenceLocation.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
556                 public void run() {
557                     FileLock lock = null;
558                     try {
559                         // need to create with a different extension and then rename due to issue 95675
560
// should be removed when that issue gets resolved
561
dd[0] = FileUtil.copyFile(Repository.getDefault().getDefaultFileSystem().findResource(
562                                 "org-netbeans-modules-j2ee-persistence/persistence-1.0.xml"), persistenceLocation, "persistence", "xml-jpa"); //NOI18N
563
lock = dd[0].lock();
564                         dd[0].rename(lock, "persistence", "xml"); //NO18N
565
} catch (IOException JavaDoc ex) {
566                         ErrorManager.getDefault().notify(ex);
567                     } finally{
568                         if (lock != null){
569                             lock.releaseLock();
570                         }
571                     }
572                 }
573             });
574         } catch (IOException JavaDoc ex) {
575             ErrorManager.getDefault().notify(ex);
576         }
577         return dd[0];
578     }
579     
580     /**
581      * Checks whether the given project has a persistence.xml that contains at least one
582      * persistence unit.
583      *
584      * @project the project; must not be null.
585      *
586      * @return true if the given project has a persistence.xml containing
587      * at least one persitence unit, false otherwise.
588      *
589      * @throws InvalidPersistenceXmlException if the given <code>project</code> has an
590      * invalid persistence.xml file.
591      */

592     public static boolean persistenceExists(Project project) throws InvalidPersistenceXmlException{
593        Parameters.notNull("project", project); //NO18N
594

595         if (getDDFile(project) == null){
596             return false;
597         }
598         PUDataObject pud = getPUDataObject(project);
599         return pud.getPersistence().getPersistenceUnit().length > 0;
600     }
601     
602     /**
603      * @return persistence.xml descriptor of first MetadataUnit found on project or null if none found
604      */

605     public static FileObject getDDFile(Project project){
606         PersistenceScope[] persistenceScopes = PersistenceUtils.getPersistenceScopes(project);
607         for (int i = 0; i < persistenceScopes.length; i++) {
608             return persistenceScopes[i].getPersistenceXml();
609         }
610         return null;
611     }
612     
613     
614     /**
615      * @return array of providers known to the IDE.
616      */

617     public static Provider[] getAllProviders() {
618         return new Provider[]{TOPLINK_PROVIDER, HIBERNATE_PROVIDER, KODO_PROVIDER};
619     }
620     
621     /**
622      * Makes the given persistence unit portable if possible, i.e. removes the provider class from it.
623      * A persistence unit may be made portable if it uses the default provider of the project's target
624      * server, it doesn't specify any properties and it is not defined in Java SE environment.
625      *
626      * @param project the project in which the given persistence unit is defined. Must not be null.
627      * @param persistenceUnit the persistence unit to be made portable. Must not be null.
628      *
629      * @return true if given persistence unit could be made portable, false otherwise.
630      *
631      * @throws NullPointerException if either project or persistenceUnit was null.
632      */

633     public static boolean makePortableIfPossible(Project project, PersistenceUnit persistenceUnit){
634         
635         Parameters.notNull("project", project); //NO18N
636
Parameters.notNull("persistenceUnit", persistenceUnit); //NO18N
637

638         if (Util.isJavaSE(project)){
639             return false;
640         }
641         
642         Provider defaultProvider = getContainerManagedProvider(project);
643         
644         if (defaultProvider == null){
645             return false;
646         }
647         
648         if (defaultProvider.getProviderClass().equals(persistenceUnit.getProvider())
649                 && persistenceUnit.getProperties().sizeProperty2() == 0){
650             
651             persistenceUnit.setProvider(null);
652             return true;
653         }
654         
655         return false;
656     }
657     
658     /**
659      * Checks whether the given <code>project</code>'s target server is present.
660      *
661      * @param project the project whose target server's presence is checked; must not be null.
662      * @return true if the given <code>project</code> has its target server present or
663      * if the project does not need a target server (i.e. it is not a J2EE project), false otherwise.
664      * @throws NullPointerException if the given <code>project</code> was null.
665      */

666     public static boolean isValidServerInstanceOrNone(Project project){
667         Parameters.notNull("project", project);
668         ServerStatusProvider serverStatusProvider = project.getLookup().lookup(ServerStatusProvider.class);
669         if (serverStatusProvider == null) {
670             // not a J2EE project
671
return true;
672         }
673         return serverStatusProvider.validServerInstancePresent();
674     }
675     
676 }
677
Popular Tags