xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/ClientMsgManager.java (revision 89b2a9fbeabf42fa54594df0e5927bcc50a07cc9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2001 by Sun Microsystems, Inc.
23  * All rights reserved.
24  *
25  */
26 
27 //  ClientMsgManager.java:Manages versioned client message creation in server
28 //  Author:           James Kempf
29 //  Created On:       Thu Sep 17 10:16:33 1998
30 //  Last Modified By: James Kempf
31 //  Last Modified On: Tue Oct 13 15:26:16 1998
32 //  Update Count:     8
33 //
34 
35 package com.sun.slp;
36 
37 import java.util.*;
38 
39 /**
40  * The ClientMsgManager class manages creation of client messages in the
41  * slpd server. Client messages are needed for active DA advertisement
42  * solicitation, and for forwarding of registrations and deregistrations
43  * from the SA server to DAs. This class creates the appropriately
44  * versioned message instance, based on the arguments. It also
45  * sets the header variables. It is up to the caller to set the
46  * instance variables in the object itself.
47  *
48  * @author James Kempf
49  */
50 
51 abstract class ClientMsgManager extends Object {
52 
53     // The class table contains classes registered for particular versions
54     //  and message types.
55 
56     private static Hashtable classTable = new Hashtable();
57 
58     // Register a new message type class and version.
59 
60     static void addClientMsgClass(String className,
61 				  int version,
62 				  String keyName) {
63 
64 	// Create the key.
65 
66 	String key = makeClassKey(keyName, version);
67 
68 	try {
69 
70 	    Class headerClass = Class.forName(className);
71 
72 	    classTable.put(headerClass, key);
73 
74 	} catch (ClassNotFoundException ex) {
75 
76 	    Assert.slpassert(false,
77 			  "no_class",
78 			  new Object[] {className});
79 
80 	}
81     }
82 
83     // Return the appropriately versioned object, with instance variables
84     //  set in the header.
85 
86     static SrvLocMsg
87 	newInstance(String keyName,
88 		    int version,
89 		    boolean isTCP)
90 	throws ServiceLocationException {
91 
92 	try {
93 
94 	    // Get header class.
95 
96 	    Class msgClass =
97 		(Class)classTable.get(makeClassKey(keyName, version));
98 
99 	    if (msgClass == null) {
100 		throw
101 		    new ServiceLocationException(
102 				ServiceLocationException.INTERNAL_ERROR,
103 				"cmm_creation_error",
104 				new Object[] { keyName,
105 						   new Integer(version)});
106 
107 	    }
108 
109 	    SrvLocMsg msg = (SrvLocMsg)msgClass.newInstance();
110 
111 	    // Set the packet length. If we've come via TCP, we don't
112 	    //  need to set it.
113 
114 	    SrvLocHeader hdr = msg.getHeader();
115 
116 	    if (!isTCP) {
117 		hdr.packetLength = SLPConfig.getSLPConfig().getMTU();
118 
119 	    }
120 
121 	    return msg;
122 
123 	} catch (Exception ex) {
124 	    throw
125 		new ServiceLocationException(
126 				ServiceLocationException.INTERNAL_ERROR,
127 				"cmm_creation_exception",
128 				new Object[] { ex,
129 						   keyName,
130 						   new Integer(version),
131 						   ex.getMessage()});
132 	}
133     }
134 
135     // Create the key for the hashtable.
136 
137     private static String makeClassKey(String className, int version) {
138 
139 	return className + version;
140     }
141 
142 }
143