2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.apache.log4j.net;
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
22 import java.util.HashMap;
23 import java.util.Hashtable;
26 import org.apache.log4j.helpers.LogLog;
28 public class ZeroConfSupport {
29 private static Object jmDNS = initializeJMDNS();
32 private static Class jmDNSClass;
33 private static Class serviceInfoClass;
35 public ZeroConfSupport(String zone, int port, String name, Map properties) {
36 //if version 3 is available, use it to constuct a serviceInfo instance, otherwise support the version1 API
37 boolean isVersion3 = false;
39 //create method is in version 3, not version 1
40 jmDNSClass.getMethod("create", null);
42 } catch (NoSuchMethodException e) {
47 LogLog.debug("using JmDNS version 3 to construct serviceInfo instance");
48 serviceInfo = buildServiceInfoVersion3(zone, port, name, properties);
50 LogLog.debug("using JmDNS version 1.0 to construct serviceInfo instance");
51 serviceInfo = buildServiceInfoVersion1(zone, port, name, properties);
55 public ZeroConfSupport(String zone, int port, String name) {
56 this(zone, port, name, new HashMap());
59 private static Object createJmDNSVersion1()
62 return jmDNSClass.newInstance();
63 } catch (InstantiationException e) {
64 LogLog.warn("Unable to instantiate JMDNS", e);
65 } catch (IllegalAccessException e) {
66 LogLog.warn("Unable to instantiate JMDNS", e);
71 private static Object createJmDNSVersion3()
74 Method jmDNSCreateMethod = jmDNSClass.getMethod("create", null);
75 return jmDNSCreateMethod.invoke(null, null);
76 } catch (IllegalAccessException e) {
77 LogLog.warn("Unable to instantiate jmdns class", e);
78 } catch (NoSuchMethodException e) {
79 LogLog.warn("Unable to access constructor", e);
80 } catch (InvocationTargetException e) {
81 LogLog.warn("Unable to call constructor", e);
86 private Object buildServiceInfoVersion1(String zone, int port, String name, Map properties) {
87 //version 1 uses a hashtable
88 Hashtable hashtableProperties = new Hashtable(properties);
90 Class[] args = new Class[6];
91 args[0] = String.class;
92 args[1] = String.class;
94 args[3] = int.class; //weight (0)
95 args[4] = int.class; //priority (0)
96 args[5] = Hashtable.class;
97 Constructor constructor = serviceInfoClass.getConstructor(args);
98 Object[] values = new Object[6];
101 values[2] = new Integer(port);
102 values[3] = new Integer(0);
103 values[4] = new Integer(0);
104 values[5] = hashtableProperties;
105 Object result = constructor.newInstance(values);
106 LogLog.debug("created serviceinfo: " + result);
108 } catch (IllegalAccessException e) {
109 LogLog.warn("Unable to construct ServiceInfo instance", e);
110 } catch (NoSuchMethodException e) {
111 LogLog.warn("Unable to get ServiceInfo constructor", e);
112 } catch (InstantiationException e) {
113 LogLog.warn("Unable to construct ServiceInfo instance", e);
114 } catch (InvocationTargetException e) {
115 LogLog.warn("Unable to construct ServiceInfo instance", e);
120 private Object buildServiceInfoVersion3(String zone, int port, String name, Map properties) {
122 Class[] args = new Class[6];
123 args[0] = String.class; //zone/type
124 args[1] = String.class; //display name
125 args[2] = int.class; //port
126 args[3] = int.class; //weight (0)
127 args[4] = int.class; //priority (0)
129 Method serviceInfoCreateMethod = serviceInfoClass.getMethod("create", args);
130 Object[] values = new Object[6];
133 values[2] = new Integer(port);
134 values[3] = new Integer(0);
135 values[4] = new Integer(0);
136 values[5] = properties;
137 Object result = serviceInfoCreateMethod.invoke(null, values);
138 LogLog.debug("created serviceinfo: " + result);
140 } catch (IllegalAccessException e) {
141 LogLog.warn("Unable to invoke create method", e);
142 } catch (NoSuchMethodException e) {
143 LogLog.warn("Unable to find create method", e);
144 } catch (InvocationTargetException e) {
145 LogLog.warn("Unable to invoke create method", e);
150 public void advertise() {
152 Method method = jmDNSClass.getMethod("registerService", new Class[]{serviceInfoClass});
153 method.invoke(jmDNS, new Object[]{serviceInfo});
154 LogLog.debug("registered serviceInfo: " + serviceInfo);
155 } catch(IllegalAccessException e) {
156 LogLog.warn("Unable to invoke registerService method", e);
157 } catch(NoSuchMethodException e) {
158 LogLog.warn("No registerService method", e);
159 } catch(InvocationTargetException e) {
160 LogLog.warn("Unable to invoke registerService method", e);
164 public void unadvertise() {
166 Method method = jmDNSClass.getMethod("unregisterService", new Class[]{serviceInfoClass});
167 method.invoke(jmDNS, new Object[]{serviceInfo});
168 LogLog.debug("unregistered serviceInfo: " + serviceInfo);
169 } catch(IllegalAccessException e) {
170 LogLog.warn("Unable to invoke unregisterService method", e);
171 } catch(NoSuchMethodException e) {
172 LogLog.warn("No unregisterService method", e);
173 } catch(InvocationTargetException e) {
174 LogLog.warn("Unable to invoke unregisterService method", e);
178 private static Object initializeJMDNS() {
180 jmDNSClass = Class.forName("javax.jmdns.JmDNS");
181 serviceInfoClass = Class.forName("javax.jmdns.ServiceInfo");
182 } catch (ClassNotFoundException e) {
183 LogLog.warn("JmDNS or serviceInfo class not found", e);
186 //if version 3 is available, use it to constuct a serviceInfo instance, otherwise support the version1 API
187 boolean isVersion3 = false;
189 //create method is in version 3, not version 1
190 jmDNSClass.getMethod("create", null);
192 } catch (NoSuchMethodException e) {
197 return createJmDNSVersion3();
199 return createJmDNSVersion1();
203 public static Object getJMDNSInstance() {