1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <assert.h>
30*0Sstevel@tonic-gate #include <synch.h>
31*0Sstevel@tonic-gate #include <jni.h>
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #include "class_cache.h"
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate * Only certain classes are deemed worthy of caching. These be them.
37*0Sstevel@tonic-gate */
38*0Sstevel@tonic-gate #define DCR_NAME "com/sun/dhcpmgr/data/DhcpClientRecord"
39*0Sstevel@tonic-gate #define DTR_NAME "com/sun/dhcpmgr/data/DhcptabRecord"
40*0Sstevel@tonic-gate #define NET_NAME "com/sun/dhcpmgr/data/Network"
41*0Sstevel@tonic-gate #define MAC_NAME "com/sun/dhcpmgr/data/Macro"
42*0Sstevel@tonic-gate #define OPT_NAME "com/sun/dhcpmgr/data/Option"
43*0Sstevel@tonic-gate #define DS_NAME "com/sun/dhcpmgr/data/DhcpDatastore"
44*0Sstevel@tonic-gate #define CFG_NAME "com/sun/dhcpmgr/data/DhcpdOptions"
45*0Sstevel@tonic-gate #define RES_NAME "com/sun/dhcpmgr/data/DhcpResource"
46*0Sstevel@tonic-gate #define IP_NAME "com/sun/dhcpmgr/data/IPAddress"
47*0Sstevel@tonic-gate #define IPIF_NAME "com/sun/dhcpmgr/data/IPInterface"
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate * As with classes, only certain methods are cached.
51*0Sstevel@tonic-gate */
52*0Sstevel@tonic-gate #define DCR_CONS_NAME "<init>"
53*0Sstevel@tonic-gate #define DCR_GETCID_NAME "getClientId"
54*0Sstevel@tonic-gate #define DCR_GETFLAG_NAME "getFlagString"
55*0Sstevel@tonic-gate #define DCR_GETCIP_NAME "getClientIPAddress"
56*0Sstevel@tonic-gate #define DCR_GETSIP_NAME "getServerIPAddress"
57*0Sstevel@tonic-gate #define DCR_GETEXP_NAME "getExpirationTime"
58*0Sstevel@tonic-gate #define DCR_GETSIG_NAME "getSignature"
59*0Sstevel@tonic-gate #define DCR_GETMAC_NAME "getMacro"
60*0Sstevel@tonic-gate #define DCR_GETCMT_NAME "getComment"
61*0Sstevel@tonic-gate #define DTR_GETKEY_NAME "getKey"
62*0Sstevel@tonic-gate #define DTR_GETFLAG_NAME "getFlag"
63*0Sstevel@tonic-gate #define DTR_GETSIG_NAME "getSignature"
64*0Sstevel@tonic-gate #define DTR_GETVAL_NAME "getValue"
65*0Sstevel@tonic-gate #define NET_CONS_NAME "<init>"
66*0Sstevel@tonic-gate #define MAC_CONS_NAME "<init>"
67*0Sstevel@tonic-gate #define OPT_CONS_NAME "<init>"
68*0Sstevel@tonic-gate #define DS_CONS_NAME "<init>"
69*0Sstevel@tonic-gate #define DS_GETRSRC_NAME "getResource"
70*0Sstevel@tonic-gate #define DS_GETLOC_NAME "getLocation"
71*0Sstevel@tonic-gate #define DS_GETRSRCCFG_NAME "getConfig"
72*0Sstevel@tonic-gate #define DS_GETVER_NAME "getVersion"
73*0Sstevel@tonic-gate #define CFG_CONS_NAME "<init>"
74*0Sstevel@tonic-gate #define CFG_SET_NAME "set"
75*0Sstevel@tonic-gate #define CFG_GETALL_NAME "getAll"
76*0Sstevel@tonic-gate #define RES_GETKEY_NAME "getKey"
77*0Sstevel@tonic-gate #define RES_GETVAL_NAME "getValue"
78*0Sstevel@tonic-gate #define RES_ISCOM_NAME "isComment"
79*0Sstevel@tonic-gate #define IP_CONS_NAME "<init>"
80*0Sstevel@tonic-gate #define IPIF_CONS_NAME "<init>"
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate * Signatures for the methods can be found below.
84*0Sstevel@tonic-gate */
85*0Sstevel@tonic-gate #define DCR_CONS_SIG "(Ljava/lang/String;Ljava/lang/String;"\
86*0Sstevel@tonic-gate "Ljava/lang/String;Ljava/lang/String;"\
87*0Sstevel@tonic-gate "Ljava/lang/String;Ljava/lang/String;"\
88*0Sstevel@tonic-gate "Ljava/lang/String;Ljava/lang/String;)V"
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate #define DCR_GETCID_SIG "()Ljava/lang/String;"
91*0Sstevel@tonic-gate #define DCR_GETFLAG_SIG "()Ljava/lang/String;"
92*0Sstevel@tonic-gate #define DCR_GETCIP_SIG "()Ljava/lang/String;"
93*0Sstevel@tonic-gate #define DCR_GETSIP_SIG "()Ljava/lang/String;"
94*0Sstevel@tonic-gate #define DCR_GETEXP_SIG "()Ljava/lang/String;"
95*0Sstevel@tonic-gate #define DCR_GETSIG_SIG "()Ljava/lang/String;"
96*0Sstevel@tonic-gate #define DCR_GETMAC_SIG "()Ljava/lang/String;"
97*0Sstevel@tonic-gate #define DCR_GETCMT_SIG "()Ljava/lang/String;"
98*0Sstevel@tonic-gate #define DTR_GETKEY_SIG "()Ljava/lang/String;"
99*0Sstevel@tonic-gate #define DTR_GETFLAG_SIG "()Ljava/lang/String;"
100*0Sstevel@tonic-gate #define DTR_GETSIG_SIG "()Ljava/lang/String;"
101*0Sstevel@tonic-gate #define DTR_GETVAL_SIG "()Ljava/lang/String;"
102*0Sstevel@tonic-gate #define NET_CONS_SIG "(Ljava/lang/String;I)V"
103*0Sstevel@tonic-gate #define MAC_CONS_SIG "(Ljava/lang/String;Ljava/lang/String;"\
104*0Sstevel@tonic-gate "Ljava/lang/String;)V"
105*0Sstevel@tonic-gate #define OPT_CONS_SIG "(Ljava/lang/String;B[Ljava/lang/String;"\
106*0Sstevel@tonic-gate "SBIILjava/lang/String;Z)V"
107*0Sstevel@tonic-gate #define DS_CONS_SIG "(Ljava/lang/String;IZ)V"
108*0Sstevel@tonic-gate #define DS_GETRSRC_SIG "()Ljava/lang/String;"
109*0Sstevel@tonic-gate #define DS_GETLOC_SIG "()Ljava/lang/String;"
110*0Sstevel@tonic-gate #define DS_GETRSRCCFG_SIG "()Ljava/lang/String;"
111*0Sstevel@tonic-gate #define DS_GETVER_SIG "()I"
112*0Sstevel@tonic-gate #define CFG_CONS_SIG "()V"
113*0Sstevel@tonic-gate #define CFG_SET_SIG "(Ljava/lang/String;Ljava/lang/String;Z)V"
114*0Sstevel@tonic-gate #define CFG_GETALL_SIG "()[Ljava/lang/Object;"
115*0Sstevel@tonic-gate #define RES_GETKEY_SIG "()Ljava/lang/String;"
116*0Sstevel@tonic-gate #define RES_GETVAL_SIG "()Ljava/lang/String;"
117*0Sstevel@tonic-gate #define RES_ISCOM_SIG "()Z"
118*0Sstevel@tonic-gate #define IP_CONS_SIG "(Ljava/lang/String;)V"
119*0Sstevel@tonic-gate #define IPIF_CONS_SIG "(Ljava/lang/String;Ljava/lang/String;"\
120*0Sstevel@tonic-gate "Ljava/lang/String;)V"
121*0Sstevel@tonic-gate /*
122*0Sstevel@tonic-gate * Class map.
123*0Sstevel@tonic-gate */
124*0Sstevel@tonic-gate typedef struct {
125*0Sstevel@tonic-gate jclass cl_class;
126*0Sstevel@tonic-gate char *cl_name;
127*0Sstevel@tonic-gate } cl_map_t;
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate * Note that the order of the entries in this table must match
131*0Sstevel@tonic-gate * exactly with the CC_CLASSMAP_ID enumeration in class_cache.h.
132*0Sstevel@tonic-gate */
133*0Sstevel@tonic-gate static cl_map_t classMap[] = {
134*0Sstevel@tonic-gate { NULL, DCR_NAME }, /* DCR_CLASS */
135*0Sstevel@tonic-gate { NULL, DTR_NAME }, /* DTR_CLASS */
136*0Sstevel@tonic-gate { NULL, NET_NAME }, /* NET_CLASS */
137*0Sstevel@tonic-gate { NULL, MAC_NAME }, /* MAC_CLASS */
138*0Sstevel@tonic-gate { NULL, OPT_NAME }, /* OPT_CLASS */
139*0Sstevel@tonic-gate { NULL, DS_NAME }, /* DS_CLASS */
140*0Sstevel@tonic-gate { NULL, CFG_NAME }, /* CFG_CLASS */
141*0Sstevel@tonic-gate { NULL, RES_NAME }, /* RES_CLASS */
142*0Sstevel@tonic-gate { NULL, IP_NAME }, /* IP_CLASS */
143*0Sstevel@tonic-gate { NULL, IPIF_NAME } /* IPIF_CLASS */
144*0Sstevel@tonic-gate };
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate /*
147*0Sstevel@tonic-gate * Method ID map.
148*0Sstevel@tonic-gate */
149*0Sstevel@tonic-gate typedef struct {
150*0Sstevel@tonic-gate jmethodID mi_methodID;
151*0Sstevel@tonic-gate char *mi_name;
152*0Sstevel@tonic-gate char *mi_signature;
153*0Sstevel@tonic-gate } mi_map_t;
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate /*
156*0Sstevel@tonic-gate * Note that the order of the entries in this table must match
157*0Sstevel@tonic-gate * exactly with the CC_METHODMAP_ID enumeration in class_cache.h.
158*0Sstevel@tonic-gate */
159*0Sstevel@tonic-gate static mi_map_t methodIDMap[] = {
160*0Sstevel@tonic-gate { NULL, DCR_CONS_NAME, DCR_CONS_SIG }, /* DCR_CONS */
161*0Sstevel@tonic-gate { NULL, DCR_GETCID_NAME, DCR_GETCID_SIG }, /* DCR_GETCID */
162*0Sstevel@tonic-gate { NULL, DCR_GETFLAG_NAME, DCR_GETFLAG_SIG }, /* DCR_GETFLAG */
163*0Sstevel@tonic-gate { NULL, DCR_GETCIP_NAME, DCR_GETCIP_SIG }, /* DCR_GETCIP */
164*0Sstevel@tonic-gate { NULL, DCR_GETSIP_NAME, DCR_GETSIP_SIG }, /* DCR_GETSIP */
165*0Sstevel@tonic-gate { NULL, DCR_GETEXP_NAME, DCR_GETEXP_SIG }, /* DCR_GETEXP */
166*0Sstevel@tonic-gate { NULL, DCR_GETSIG_NAME, DCR_GETSIG_SIG }, /* DCR_GETSIG */
167*0Sstevel@tonic-gate { NULL, DCR_GETMAC_NAME, DCR_GETMAC_SIG }, /* DCR_GETMAC */
168*0Sstevel@tonic-gate { NULL, DCR_GETCMT_NAME, DCR_GETCMT_SIG }, /* DCR_GETCMT */
169*0Sstevel@tonic-gate { NULL, DTR_GETKEY_NAME, DTR_GETKEY_SIG }, /* DTR_GETKEY */
170*0Sstevel@tonic-gate { NULL, DTR_GETFLAG_NAME, DTR_GETFLAG_SIG }, /* DTR_GETFLAG */
171*0Sstevel@tonic-gate { NULL, DTR_GETSIG_NAME, DTR_GETSIG_SIG }, /* DTR_GETSIG */
172*0Sstevel@tonic-gate { NULL, DTR_GETVAL_NAME, DTR_GETVAL_SIG }, /* DTR_GETVAL */
173*0Sstevel@tonic-gate { NULL, NET_CONS_NAME, NET_CONS_SIG }, /* NET_CONS */
174*0Sstevel@tonic-gate { NULL, MAC_CONS_NAME, MAC_CONS_SIG }, /* MAC_CONS */
175*0Sstevel@tonic-gate { NULL, OPT_CONS_NAME, OPT_CONS_SIG }, /* OPT_CONS */
176*0Sstevel@tonic-gate { NULL, DS_CONS_NAME, DS_CONS_SIG }, /* DS_CONS */
177*0Sstevel@tonic-gate { NULL, DS_GETRSRC_NAME, DS_GETRSRC_SIG }, /* DS_GETRSRC */
178*0Sstevel@tonic-gate { NULL, DS_GETLOC_NAME, DS_GETLOC_SIG }, /* DS_GETLOC */
179*0Sstevel@tonic-gate { NULL, DS_GETRSRCCFG_NAME, DS_GETRSRCCFG_SIG }, /* DS_GETRSRCCFG */
180*0Sstevel@tonic-gate { NULL, DS_GETVER_NAME, DS_GETVER_SIG }, /* DS_GETVER */
181*0Sstevel@tonic-gate { NULL, CFG_CONS_NAME, CFG_CONS_SIG }, /* CFG_CONS */
182*0Sstevel@tonic-gate { NULL, CFG_SET_NAME, CFG_SET_SIG }, /* CFG_SET */
183*0Sstevel@tonic-gate { NULL, CFG_GETALL_NAME, CFG_GETALL_SIG }, /* CFG_GETALL */
184*0Sstevel@tonic-gate { NULL, RES_GETKEY_NAME, RES_GETKEY_SIG }, /* RES_GETKEY */
185*0Sstevel@tonic-gate { NULL, RES_GETVAL_NAME, RES_GETVAL_SIG }, /* RES_GETVAL */
186*0Sstevel@tonic-gate { NULL, RES_ISCOM_NAME, RES_ISCOM_SIG }, /* RES_ISCOM */
187*0Sstevel@tonic-gate { NULL, IP_CONS_NAME, IP_CONS_SIG }, /* IP_CONS */
188*0Sstevel@tonic-gate { NULL, IPIF_CONS_NAME, IPIF_CONS_SIG } /* IPIF_CONS */
189*0Sstevel@tonic-gate };
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate * The locks to protect the class and method maps.
193*0Sstevel@tonic-gate */
194*0Sstevel@tonic-gate static mutex_t cmap_lock;
195*0Sstevel@tonic-gate static mutex_t mmap_lock;
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate void
init_class_cache(void)198*0Sstevel@tonic-gate init_class_cache(void) {
199*0Sstevel@tonic-gate (void) mutex_init(&cmap_lock, USYNC_THREAD, NULL);
200*0Sstevel@tonic-gate (void) mutex_init(&mmap_lock, USYNC_THREAD, NULL);
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate /*
204*0Sstevel@tonic-gate * Get a dhcpmgr class from the cache.
205*0Sstevel@tonic-gate */
206*0Sstevel@tonic-gate jclass
find_class(JNIEnv * env,CC_CLASSMAP_ID id)207*0Sstevel@tonic-gate find_class(JNIEnv *env, CC_CLASSMAP_ID id) {
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gate jclass *class;
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate assert(id >= 0 && id <= CC_CLASSMAP_NUM);
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gate /*
214*0Sstevel@tonic-gate * If the class has not been cached yet, go find it and cache it.
215*0Sstevel@tonic-gate */
216*0Sstevel@tonic-gate class = &classMap[id].cl_class;
217*0Sstevel@tonic-gate if (*class == NULL) {
218*0Sstevel@tonic-gate /*
219*0Sstevel@tonic-gate * Check again with the lock held this time.
220*0Sstevel@tonic-gate */
221*0Sstevel@tonic-gate (void) mutex_lock(&cmap_lock);
222*0Sstevel@tonic-gate if (*class == NULL) {
223*0Sstevel@tonic-gate char *name = classMap[id].cl_name;
224*0Sstevel@tonic-gate jclass local = (*env)->FindClass(env, name);
225*0Sstevel@tonic-gate if (local != NULL) {
226*0Sstevel@tonic-gate *class = (*env)->NewGlobalRef(env, local);
227*0Sstevel@tonic-gate (*env)->DeleteLocalRef(env, local);
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate (void) mutex_unlock(&cmap_lock);
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate return (*class);
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate /*
237*0Sstevel@tonic-gate * Get a dhcpmgr class methodid from the cache.
238*0Sstevel@tonic-gate */
239*0Sstevel@tonic-gate jmethodID
get_methodID(JNIEnv * env,jclass class,CC_METHODMAP_ID id)240*0Sstevel@tonic-gate get_methodID(JNIEnv *env, jclass class, CC_METHODMAP_ID id) {
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate jmethodID *methodID;
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate assert(id >= 0 && id <= CC_METHODMAP_NUM);
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate /*
247*0Sstevel@tonic-gate * If the methodID has not been cached, go find it and cache it.
248*0Sstevel@tonic-gate */
249*0Sstevel@tonic-gate methodID = &methodIDMap[id].mi_methodID;
250*0Sstevel@tonic-gate if (*methodID == NULL) {
251*0Sstevel@tonic-gate /*
252*0Sstevel@tonic-gate * Check again with the lock held this time.
253*0Sstevel@tonic-gate */
254*0Sstevel@tonic-gate (void) mutex_lock(&mmap_lock);
255*0Sstevel@tonic-gate if (*methodID == NULL) {
256*0Sstevel@tonic-gate char *name = methodIDMap[id].mi_name;
257*0Sstevel@tonic-gate char *signature = methodIDMap[id].mi_signature;
258*0Sstevel@tonic-gate *methodID = (*env)->GetMethodID(env, class, name,
259*0Sstevel@tonic-gate signature);
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate (void) mutex_unlock(&mmap_lock);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate return (*methodID);
265*0Sstevel@tonic-gate }
266