1package CyrusSasl;
2
3import java.util.Hashtable;
4import javax.security.auth.callback.*;
5
6class ServerFactory implements SaslServerFactory
7{
8    private int localptr = 0;
9
10    /* JNI functions  */
11    private native int jni_sasl_server_init(String appname);
12    private native int jni_sasl_server_new(String service,
13					   String local_domain,
14					   int secflags);
15
16
17    public ServerFactory()
18    {
19	/* these parameters aren't needed for getting mech list */
20	localptr = jni_sasl_server_new("foo", "bar", 0);
21    }
22
23    private boolean init(String appname)
24    {
25	/* load library */
26	try {
27	    System.loadLibrary("javasasl");
28	} catch (UnsatisfiedLinkError e) {
29	    /* xxx */
30	    System.out.println("Unable to load javasasl library");
31	}
32
33	jni_sasl_server_init(appname);
34
35	return true;
36    }
37
38    {
39	init("javasasl application");
40    }
41
42    public SaslServer createSaslServer(String mechanism,
43				       String protocol,
44				       String serverName,
45				       Hashtable props,
46				       javax.security.auth.callback.CallbackHandler cbh)
47	throws SaslException
48    {
49	int cptr;
50
51	cptr = jni_sasl_server_new(protocol,
52				   serverName,
53				   0);
54
55	if (cptr == 0) {
56	    throw new SaslException("Unable to create new Client connection object",
57				    new Throwable());
58	}
59
60	return new GenericServer(cptr,mechanism,props,cbh);
61    }
62
63    private native String jni_sasl_server_getlist(int ptr, String prefix,
64						  String sep, String suffix);
65
66    public String[] getMechanismNames()
67    {
68	if (localptr == 0)
69	    localptr = jni_sasl_server_new("foo",
70					   "bar",
71					   0);
72
73	String list = jni_sasl_server_getlist(localptr, "",
74					      "\n","\n");
75
76	/* count newlines */
77	int newlines = 0;
78	int pos =0;
79
80	while (pos < list.length()) {
81	    if (list.charAt(pos)=='\n')
82		newlines++;
83	    pos++;
84	}
85
86	String[]ret = new String[newlines];
87
88	int num =0;
89	pos =0;
90	String temp="";
91
92	while (pos < list.length()) {
93	    if (list.charAt(pos)=='\n') {
94		ret[num++]=temp;
95		temp=new String("");
96	    } else {
97		temp+=list.charAt(pos);
98	    }
99	    pos++;
100	}
101
102	return ret;
103    }
104
105}
106