001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.webengine.jaxrs; 020 021import java.io.IOException; 022import java.util.HashMap; 023import java.util.Map; 024import java.util.Set; 025 026import javax.ws.rs.core.Application; 027 028import org.osgi.framework.Bundle; 029 030/** 031 * A wrapper for a JAX-RS application fragment declared in manifest. The fragment application will be added to the 032 * target host application. 033 * 034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 035 */ 036public class ApplicationFragment extends Application { 037 038 protected final String hostName; 039 040 protected final Bundle bundle; 041 042 protected final Map<String, String> attrs; 043 044 protected String appClass; 045 046 private volatile Application app; 047 048 public static Map<String, String> createAttributes(String hostName) { 049 HashMap<String, String> attrs = new HashMap<String, String>(); 050 if (hostName != null) { 051 attrs.put("host", hostName); 052 } 053 return attrs; 054 } 055 056 public ApplicationFragment(Bundle bundle, String appClass) { 057 this(bundle, appClass, (String) null); 058 } 059 060 public ApplicationFragment(Bundle bundle, String appClass, String host) { 061 this(bundle, appClass, createAttributes(host)); 062 } 063 064 public ApplicationFragment(Bundle bundle, String appClass, Map<String, String> attrs) { 065 this.bundle = bundle; 066 this.appClass = appClass; 067 this.attrs = attrs; 068 String host = attrs.get("host"); 069 this.hostName = host == null ? "default" : host; 070 } 071 072 protected synchronized void createApp() { 073 try { 074 Object obj = bundle.loadClass(appClass).newInstance(); 075 if (obj instanceof ApplicationFactory) { 076 app = ((ApplicationFactory) obj).getApplication(bundle, attrs); 077 } else if (obj instanceof Application) { 078 app = (Application) obj; 079 } else { 080 throw new IllegalArgumentException("Expecting an Application or ApplicationFactory class: " + appClass); 081 } 082 } catch (ReflectiveOperationException | IOException e) { 083 String msg = "Cannot instantiate JAX-RS application " + appClass + " from bundle " 084 + bundle.getSymbolicName(); 085 throw new RuntimeException(msg, e); 086 } 087 } 088 089 public synchronized void reload() { 090 app = null; 091 } 092 093 public Bundle getBundle() { 094 return bundle; 095 } 096 097 public Map<String, String> getAttrs() { 098 return attrs; 099 } 100 101 public String getHostName() { 102 return hostName; 103 } 104 105 public Application get() { 106 if (app == null) { 107 createApp(); 108 } 109 return app; 110 } 111 112 @Override 113 public Set<Class<?>> getClasses() { 114 return get().getClasses(); 115 } 116 117 @Override 118 public Set<Object> getSingletons() { 119 return get().getSingletons(); 120 } 121 122}