Bonjour,
Je débute sur un mini-projet qui est architecturé comme suit :
DB Oracle, Serveur Glassfish 2.1 et IDE Netbeans 6.5. Tout se passe dans un premier temps en localhost, mais la condition est qu'il faut séparer les projets : 1 projets pur EJB et 1 projet client qui va aller faire du CRUD.
J'ai créé un premier projet EJB qui parvient bien à encapsuler avec des Entity deux tables (c'est basique mais c'est un mini-projet) de la DB Oracle. Le pool de connexion marche bien. Ce projet comporte un fichier xml de persistence.
J'ai ensuite créé un second projet que j'appelle "client". Dans ce projet, j'ai créé une classe BusinessDelegate liée à la table Personne
Et le service locator suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public class PersonneDelegate { // ====================================== // = Personne Business methods = // ====================================== public static void createPersonne(Personne personne) { getPersonneRemote().create(personne); } public static Personne findPersonne(Long personneId) { return getPersonneRemote().find(personneId); } public static void deletePersonne(Personne personne) { getPersonneRemote().remove(personne); } public static void updatePersonne(Personne personne) { getPersonneRemote().edit(personne); } public static List<Personne> findPersonnes() { return getPersonneRemote().findAll(); } // ====================================== // = Private methods = // ====================================== private static PersonneFacadeLocal getPersonneRemote() { PersonneFacadeLocal personneFacadeLocal = null; personneFacadeLocal = (PersonneFacadeLocal) ServiceLocator.getInstance().getRemoteInterface("Personne"); return personneFacadeLocal; } }
J'ai lié les deux projets et j'ai compris qu'il fallait passer au constructeur de l'InitialContext des informations :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class ServiceLocator { // ====================================== // = Attributes = // ====================================== private Context initialContext; private Map<String, Object> cache; // ====================================== // = Singleton = // ====================================== private static ServiceLocator instance = null; public static ServiceLocator getInstance() { if (instance == null) instance = new ServiceLocator(); return instance; } private ServiceLocator() throws ServiceLocatorException { try { initialContext = new InitialContext(PropertyGlassfish.getInstance().getProperties()); cache = new HashMap<String, Object>(); } catch (Exception e) { throw new ServiceLocatorException(e); } } // ====================================== // = Business methods = // ====================================== /** * will get the ejb Local home factory. If this ejb home factory has already been * clients need to cast to the type of EJBHome they desire * * @param jndiName JNDI name of the EJB that we are looking for * @return the EJB Home corresponding to the homeName * @throws ServiceLocatorException thrown if lookup problems */ public Object getRemoteInterface(String jndiName) throws ServiceLocatorException { Object remoteInterface = getRemoteObject(jndiName); return remoteInterface; } // ====================================== // = Private methods = // ====================================== private synchronized Object getRemoteObject(String jndiName) throws ServiceLocatorException { Object remoteObject = cache.get(jndiName); if (remoteObject == null) { try { remoteObject = initialContext.lookup(jndiName); cache.put(jndiName, remoteObject); } catch (Exception e) { throw new ServiceLocatorException(e); } } return remoteObject; }
J'ai ajouté des librairies au projet client et j'ai créé un main comme suit :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class PropertyGlassfish { private Properties props = null; // ====================================== // = Singleton = // ====================================== private static PropertyGlassfish instance = null; public static PropertyGlassfish getInstance() { if (instance == null) instance = new PropertyGlassfish(); return instance; } public Properties getProperties() { props = new Properties(); props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory"); props.setProperty("java.naming.factory.url.pkgs","com.sun.enterprise.naming"); props.setProperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl"); props.setProperty("java.naming.provider.url","localhost"); return props; } }
J'ai fait grâce des différents "import" de début de classe pour alléger le code.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 public class TestClient { public static void main(String... strings) { List<Personne> list = PersonneDelegate.findPersonnes(); Iterator it = list.iterator(); while(it.hasNext()) System.out.println(it.next()); } }
Quand je lance le main, il y a une erreur :
Je pense qu'il doit manquer un autre fichier xml dans le projet client mais lequel ? où faut-il le placer ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 Exception in thread "main" locator.ServiceLocatorException: javax.naming.NameNotFoundException: Personne not found at locator.ServiceLocator.getRemoteObject(ServiceLocator.java:63) at locator.ServiceLocator.getRemoteInterface(ServiceLocator.java:49) at delegate.PersonneDelegate.getPersonneRemote(PersonneDelegate.java:41) at delegate.PersonneDelegate.findPersonnes(PersonneDelegate.java:33) at Main.TestClient.main(TestClient.java:11) Caused by: javax.naming.NameNotFoundException: Personne not found
Merci si qq'un peut m'éclairer.
Partager