KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebPersistenceProviderSupplier


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.web.project;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
27 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
28 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
29 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
30 import org.netbeans.modules.j2ee.persistence.provider.Provider;
31 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil;
32 import org.netbeans.modules.j2ee.persistence.spi.provider.PersistenceProviderSupplier;
33
34 /**
35  * An implementation of PersistenceProviderSupplier for web project.
36  *
37  * @author Erno Mononen
38  */

39 public class WebPersistenceProviderSupplier implements PersistenceProviderSupplier{
40     
41     private final WebProject project;
42     
43     /** Creates a new instance of WebPersistenceProviderSupplier */
44     public WebPersistenceProviderSupplier(WebProject project) {
45         this.project = project;
46     }
47     
48     public List JavaDoc<Provider> getSupportedProviders() {
49         // TODO: the implementation of the this method (and whole PersistenceProviderSupplier)
50
// is pretty much identical with the EJB implementation,
51
// should be refactored to some common class.
52
J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
53         J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(j2eeModuleProvider.getServerInstanceID());
54         
55         if (platform == null){
56             return Collections.<Provider>emptyList();
57         }
58         List JavaDoc<Provider> result = new ArrayList JavaDoc<Provider>();
59         
60         addPersistenceProvider(ProviderUtil.HIBERNATE_PROVIDER, "hibernatePersistenceProviderIsDefault", platform, result); // NOI18N
61
addPersistenceProvider(ProviderUtil.TOPLINK_PROVIDER, "toplinkPersistenceProviderIsDefault", platform, result);// NOI18N
62
addPersistenceProvider(ProviderUtil.KODO_PROVIDER, "kodoPersistenceProviderIsDefault", platform, result); // NOI18N
63

64         return result;
65     }
66     
67     private void addPersistenceProvider(Provider provider, String JavaDoc defaultProvider, J2eePlatform platform, List JavaDoc<Provider> providers){
68         // would need an api for this..
69
if (platform.isToolSupported(provider.getProviderClass())){
70             if (platform.isToolSupported(defaultProvider)){
71                 providers.add(0, provider);
72             } else {
73                 providers.add(provider);
74             }
75         }
76     }
77     
78     public boolean supportsDefaultProvider() {
79         
80         J2eeModuleProvider j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class);
81         J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(j2eeModuleProvider.getServerInstanceID());
82         
83         if (platform == null){
84             // server probably not registered, can't resolve whether default provider is supported (see #79856)
85
return false;
86         }
87         
88         Set JavaDoc<String JavaDoc> supportedVersions = platform.getSupportedSpecVersions(j2eeModuleProvider.getJ2eeModule().getModuleType());
89         
90         return supportedVersions.contains(J2eeModule.JAVA_EE_5);
91     }
92     
93
94 }
95
Popular Tags