jtulach@90: package org.apidesign.aserverinfo; jtulach@90: jtulach@90: import java.net.URL; jtulach@90: import java.util.concurrent.Callable; jtulach@90: import org.openide.util.Exceptions; jtulach@90: import org.openide.util.Lookup; jtulach@90: jtulach@90: public final class AServerInfo { jtulach@90: private final Callable name; jtulach@90: private final Callable url; jtulach@90: private final Callable reset; jtulach@90: private final Callable shutdown; jtulach@90: jtulach@90: public AServerInfo(Callable name, Callable url, Callable reset, Callable s) { jtulach@90: this.name = name; jtulach@90: this.url = url; jtulach@90: this.reset = reset; jtulach@90: this.shutdown = s; jtulach@90: } jtulach@90: jtulach@90: public String getName() { jtulach@90: return call(name, "noname"); jtulach@90: } jtulach@90: jtulach@90: public URL getURL() { jtulach@90: return call(url, null); jtulach@90: } jtulach@90: jtulach@90: public void reset() throws Exception { jtulach@90: reset.call(); jtulach@90: } jtulach@90: jtulach@90: public void shutdown() throws Exception { jtulach@90: shutdown.call(); jtulach@90: } jtulach@90: jtulach@90: jtulach@90: jtulach@90: private static T call(Callable name, T defValue) { jtulach@90: try { jtulach@90: return name.call(); jtulach@90: } catch (Exception ex) { jtulach@90: return defValue; jtulach@90: } jtulach@90: } jtulach@90: jtulach@90: // BEGIN: aserverinfo.create jtulach@90: public interface NameProvider { jtulach@90: public String getName(); jtulach@90: } jtulach@90: public interface URLProvider { jtulach@90: public URL getURL(); jtulach@90: } jtulach@90: public interface ResetHandler { jtulach@90: public void reset(); jtulach@90: } jtulach@90: jtulach@90: public static AServerInfo create(final Lookup interfaces) jtulach@90: // END: aserverinfo.create jtulach@90: { jtulach@90: Callable nameP = new Callable() { jtulach@90: public String call() throws Exception { jtulach@90: NameProvider p = interfaces.lookup(NameProvider.class); jtulach@90: return p == null ? "noname" : p.getName(); jtulach@90: } jtulach@90: }; jtulach@90: Callable urlP = new Callable() { jtulach@90: public URL call() throws Exception { jtulach@90: URLProvider p = interfaces.lookup(URLProvider.class); jtulach@90: return p == null ? null : p.getURL(); jtulach@90: } jtulach@90: }; jtulach@90: Callable resetP = new Callable() { jtulach@90: public Void call() throws Exception { jtulach@90: ResetHandler h = interfaces.lookup(ResetHandler.class); jtulach@90: if (h != null) { jtulach@90: h.reset(); jtulach@90: } jtulach@90: return null; jtulach@90: } jtulach@90: }; jtulach@90: jtulach@90: return new AServerInfo(nameP, urlP, resetP, null); jtulach@90: } jtulach@90: jtulach@90: // BEGIN: aserverinfo.regularcreate jtulach@90: public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset) jtulach@90: // END: aserverinfo.regularcreate jtulach@90: { jtulach@90: return create(nameProvider, urlProvider, reset, null); jtulach@90: } jtulach@90: jtulach@90: // BEGIN: aserverinfo.regularcreate.withshutdown jtulach@90: /** @since 2.0 */ jtulach@90: public interface ShutdownHandler { jtulach@90: public void shutdown(); jtulach@90: } jtulach@90: jtulach@90: /** @since 2.0 */ jtulach@90: public static AServerInfo create(NameProvider nameProvider, URLProvider urlProvider, ResetHandler reset, ShutdownHandler shutdown) jtulach@90: // END: aserverinfo.regularcreate.withshutdown jtulach@90: { jtulach@90: final NameProvider np = nameProvider; jtulach@90: final URLProvider up = urlProvider; jtulach@90: final ResetHandler h = reset; jtulach@90: final ShutdownHandler s = shutdown; jtulach@90: jtulach@90: Callable nameP = new Callable() { jtulach@90: public String call() throws Exception { jtulach@90: return np == null ? "noname" : np.getName(); jtulach@90: } jtulach@90: }; jtulach@90: Callable urlP = new Callable() { jtulach@90: public URL call() throws Exception { jtulach@90: return up == null ? null : up.getURL(); jtulach@90: } jtulach@90: }; jtulach@90: Callable resetP = new Callable() { jtulach@90: public Void call() throws Exception { jtulach@90: if (h != null) { jtulach@90: h.reset(); jtulach@90: } jtulach@90: return null; jtulach@90: } jtulach@90: }; jtulach@90: Callable shutP = new Callable() { jtulach@90: public Void call() throws Exception { jtulach@90: if (s != null) { jtulach@90: s.shutdown(); jtulach@90: } jtulach@90: return null; jtulach@90: } jtulach@90: }; jtulach@90: jtulach@90: return new AServerInfo(nameP, urlP, resetP, shutP); jtulach@90: } jtulach@90: jtulach@90: jtulach@90: jtulach@90: }