KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > msg > BindRequest


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Sofiane Chibani
22  * Contributor(s): David Feliot, Nicolas Tachker
23  */

24 package fr.dyade.aaa.jndi2.msg;
25
26 import javax.naming.*;
27 import java.io.*;
28
29 public class BindRequest extends JndiRequest {
30
31   private Object JavaDoc obj;
32
33   private boolean rebind;
34
35   public BindRequest(CompositeName name, Object JavaDoc obj)
36     throws NamingException {
37     this(name, obj, false);
38   }
39
40   public BindRequest(CompositeName name, Object JavaDoc obj, boolean rebind)
41     throws NamingException {
42     super(name);
43     if (obj == null ||
44         obj instanceof byte[] ||
45         obj instanceof Reference) {
46       this.obj = obj;
47     } else if (obj instanceof Referenceable) {
48       this.obj = ((Referenceable)obj).getReference();
49     } else {
50       this.obj = toReference(obj);
51     }
52     this.rebind = rebind;
53   }
54
55   public final Object JavaDoc getObject() {
56     return obj;
57   }
58
59   public final boolean isRebind() {
60     return rebind;
61   }
62   
63   private static Reference toReference(Object JavaDoc obj) throws NamingException {
64     ByteArrayOutputStream baos = new ByteArrayOutputStream();
65     try {
66       ObjectOutputStream oos = new ObjectOutputStream(baos);
67       oos.writeObject(obj);
68       byte[] bytes = baos.toByteArray();
69       Reference ref = new Reference(
70         obj.getClass().getName(),
71         new BinaryRefAddr(ObjectFactory.ADDRESS_TYPE, bytes),
72         "fr.dyade.aaa.jndi2.msg.ObjectFactory", null);
73       return ref;
74     } catch (Exception JavaDoc exc) {
75       NamingException ne = new NamingException();
76       ne.setRootCause(exc);
77       throw ne;
78     }
79   }
80
81   public String JavaDoc toString() {
82     return '(' + super.toString() +
83       ",rebind=" + rebind + ')';
84   }
85 }
86
Popular Tags