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