Return-Path: Delivered-To: apmail-incubator-wink-commits-archive@minotaur.apache.org Received: (qmail 57734 invoked from network); 23 Jun 2009 10:14:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 23 Jun 2009 10:14:32 -0000 Received: (qmail 73966 invoked by uid 500); 23 Jun 2009 10:14:43 -0000 Delivered-To: apmail-incubator-wink-commits-archive@incubator.apache.org Received: (qmail 73909 invoked by uid 500); 23 Jun 2009 10:14:43 -0000 Mailing-List: contact wink-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: wink-dev@incubator.apache.org Delivered-To: mailing list wink-commits@incubator.apache.org Delivered-To: moderator for wink-commits@incubator.apache.org Received: (qmail 96283 invoked by uid 99); 23 Jun 2009 05:42:40 -0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r787557 [4/12] - in /incubator/wink/contrib/ibm-jaxrs: ./ lib/ src/ src/com/ src/com/ibm/ src/com/ibm/ws/ src/com/ibm/ws/jaxrs/ src/com/ibm/ws/jaxrs/annotations/ src/com/ibm/ws/jaxrs/context/ src/com/ibm/ws/jaxrs/core/ src/com/ibm/ws/jaxrs/... Date: Tue, 23 Jun 2009 05:41:55 -0000 To: wink-commits@incubator.apache.org From: ngallardo@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090623054201.EAB4323888E4@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/IntegrationRegistry.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/IntegrationRegistry.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/IntegrationRegistry.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/IntegrationRegistry.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.integration; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CopyOnWriteArrayList; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.ibm.ws.jaxrs.i18n.Messages; + +public class IntegrationRegistry { + + private static final Log log = LogFactory.getLog(IntegrationRegistry.class); + + private static ConcurrentMap, Object>> integrationRegistry = new ConcurrentHashMap, Object>>(); + + /** + * This will add an instance of an integration provider by a class + * representing the interface that the provider implements. + * + */ + public static void addIntegrationProvider(Object key, Class providerInterface, Object integrationProvider) { + if (providerInterface == null) { + throw new IllegalArgumentException(Messages + .getMessage("invalidIntRegistryKey")); + } + addIntegrationProvider(key, providerInterface, integrationProvider, + false); + } + + /** + * This will add an instance of an integration provider by a class + * representing the interface that the provider implements. The + * 'addInList' boolean controls whether the interface has the potential + * for multiple implementations. + * + */ + public static void addIntegrationProvider(Object key, Class providerInterface, Object integrationProvider, boolean addInList) { + if (providerInterface == null) { + throw new IllegalArgumentException(Messages + .getMessage("invalidIntRegistryKey")); + } + + if (log.isDebugEnabled()) { + log.debug("Adding integration provider: " + + integrationProvider.getClass().getName() + + " for interface: " + providerInterface.getName() + + " for key: " + key); + } + + ConcurrentMap, Object> interfaceToProviderMap = integrationRegistry + .get(key); + if (interfaceToProviderMap == null) { + integrationRegistry.putIfAbsent(key, + new ConcurrentHashMap, Object>()); + interfaceToProviderMap = integrationRegistry.get(key); + } + + // handle adding the integration provider to a list + if (addInList) { + List providers = (List) interfaceToProviderMap + .get(providerInterface); + if (providers == null) { + interfaceToProviderMap.putIfAbsent(providerInterface, + new CopyOnWriteArrayList()); + providers = (List) interfaceToProviderMap + .get(providerInterface); + } + providers.add(integrationProvider); + } else { + interfaceToProviderMap.put(providerInterface, integrationProvider); + } + } + + /** + * This will return an instance of an integration provider for the class + * representing the interface implemented by the integration provider. + * + */ + public static Object getIntegrationProvider(Object key, Class providerInterface) { + if (providerInterface == null || key == null) { + throw new IllegalArgumentException(Messages + .getMessage("invalidIntRegistryKey")); + } + if (log.isDebugEnabled()) { + log.debug("Finding provider interface: " + providerInterface + + " for key: " + key); + } + Map, Object> interfaceToProviderMap = integrationRegistry + .get(key); + if (interfaceToProviderMap == null) { + if (log.isDebugEnabled()) { + log.debug("Did not find key: " + key); + } + return null; + } + Object retObj = interfaceToProviderMap.get(providerInterface); + if (log.isDebugEnabled()) { + log.debug("Get IntegrationRegistry ProviderInterface: " + retObj); + } + return retObj; + } + + /** + * Removes an integration provider for the key and the providerInterface. + * If the providerInterface is null, all integration providers are removed + * for the key. + */ + public static void removeIntegrationProvider(Object key, Class providerInterface) { + if (log.isDebugEnabled()) { + log.debug("Removing integration provider for provider interface: " + + providerInterface + " for key: " + key); + } + if (key == null) { + return; + } + + if (providerInterface == null) { + integrationRegistry.remove(key); + return; + } + + Map, Object> interfaceToProviderMap = integrationRegistry + .get(key); + if (interfaceToProviderMap != null) { + interfaceToProviderMap.remove(providerInterface); + } + } +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/JAXRSProviderCacheProvider.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/JAXRSProviderCacheProvider.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/JAXRSProviderCacheProvider.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/JAXRSProviderCacheProvider.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.integration; + +import org.apache.cxf.jaxrs.provider.ProviderFactory; + +/** + * This interface will be implemented by those components that wish to + * provide a caching mechanism for provider factories used by the runtime. + */ +public interface JAXRSProviderCacheProvider { + + /** + * This method will accept a key and an instance of ProviderFactory that is + * to be cached by the key. + */ + public void cacheProviderFactory(Object metadataKey, ProviderFactory pf); + + /** + * This method will return an instance of ProviderFactory that corresponds + * to the supplied key or null if an instance is not found. + */ + public ProviderFactory getProviderFactory(Object metadataKey); +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/MetaDataProvider.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/MetaDataProvider.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/MetaDataProvider.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/MetaDataProvider.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.integration; + +import com.ibm.ws.jaxrs.context.RESTContext; +import com.ibm.ws.jaxrs.metadata.RESTMetaData; + +/** + * This interface will be implemented by components for building RESTMetadata used by the runtime. + * + */ +public interface MetaDataProvider { + + /** + * This method will accept a key and an instance of RESTMetaData that is + * to be cached by the key. + */ + public RESTMetaData buildRESTMetaData(String metadataKey, RESTContext context) + throws Exception; + + /** + * This method will return an instance of RESTMetaData that corresponds + * to the supplied key or null if an instance is not found. + */ + public RESTMetaData getRESTMetaData(String metadataKey); + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/OptionsResponseProvider.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/OptionsResponseProvider.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/OptionsResponseProvider.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/OptionsResponseProvider.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.integration; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.ibm.ws.jaxrs.context.RESTContext; + +/** + * This interface will be implemented by those wishing to provide + * a Response object due to an OPTIONS request being sent to a + * resource within our runtime configuration. + * + */ +public interface OptionsResponseProvider { + + /** + * Indicates whether the response provider can produce a response + * based on the MIME types accepted by the client. + */ + public boolean isWriteable(MediaType acceptType); + + /** + * Creates a Response object containing the necessary content to + * fulfill an OPTIONS request for a resource or set of resources. + */ + public Response createOptionsResponse(RESTContext context); + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/ResponseWriter.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/ResponseWriter.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/ResponseWriter.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/integration/ResponseWriter.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.integration; + +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.activation.DataContentHandler; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; + +import com.ibm.ws.jaxrs.context.RESTContext; + +/** + * Implementations of this interface will handle writing a response using either + * a MessageBodyWriter or DataHandler. + * + */ +public interface ResponseWriter { + + /** + * Called before the call to the appropriate write method. This allows pre-write + * processing to be conducted. + * @param context - Response context + */ + public void preWrite(RESTContext context) throws Exception; + + /** + * Writes a response object using the supplied MessageBodyWriter. + * @param context - Response context + * @param writer - MessageBodyWriter selected by runtime + * @param responseObject - Response from the RESTful invocation + * @param clazz - Class of the object being written + * @param type - Type of the object being written + * @param annotations - Annotations found on the resource method that returned the response + * @param mediaType - Media type of the entity + * @param headers - HTTP response headers + * @param os - OutputStream for response + * @throws Exception + */ + public void writeWithEntityProvider(RESTContext context, MessageBodyWriter writer, Object responseObject, Class clazz, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap headers, OutputStream os) + throws Exception; + + /** + * Writes a response object using the supplied DataHandler. + * @param context - Response context + * @param object - Object to be serialized + * @param mimeType - Content of the output + * @param contentHandler - DataContentHandler which handles the serialization. + * @param os - OutputStream for response + * @throws Exception + */ + public void writeWithDataHandler(RESTContext context, Object object, String mimeType, DataContentHandler contentHandler, OutputStream os) + throws Exception; + + /** + * Called after the write method is called. This allows post-write processing to be conducted. + * @param context - Response context + */ + public void postWrite(RESTContext context) throws Exception; + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/ContentMonitor.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/ContentMonitor.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/ContentMonitor.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/ContentMonitor.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.io; + +/** + * This interface will be implemented by those wishing to provide + * information about the content of a resource that is returned + * by the engine. + * + */ +public interface ContentMonitor { + + /** + * Returns the content length of the returned resource. + */ + public Integer getContentLength(); + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/DelegatingOutputStream.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/DelegatingOutputStream.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/DelegatingOutputStream.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/DelegatingOutputStream.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.io; + +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * An OutputStream implementation that simply delegates to the OutputStream + * it was constructed with. + * + */ +public class DelegatingOutputStream extends OutputStream implements ContentMonitor { + + private Log log = LogFactory.getLog(DelegatingOutputStream.class); + + private OutputStream os; + + private int contentLength = -1; + + private boolean skipNextFlush = false; + + public DelegatingOutputStream(OutputStream os) { + this.os = new BufferedOutputStream(os); + contentLength = 0; + } + + @Override + public void close() throws IOException { + os.close(); + } + + @Override + public void flush() throws IOException { + if (skipNextFlush) { + if (log.isDebugEnabled()) { + log.debug("Skipping flush"); + } + skipNextFlush = false; + } else { + os.flush(); + } + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + contentLength += len; + os.write(b, off, len); + } + + @Override + public void write(byte[] b) throws IOException { + if (b != null) { + contentLength += b.length; + } + os.write(b); + } + + @Override + public void write(int b) throws IOException { + contentLength++; + os.write(b); + } + + public Integer getContentLength() { + return Integer.valueOf(contentLength); + } + + public void skipNextFlush() { + skipNextFlush = true; + } +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/LogInputStream.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/LogInputStream.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/LogInputStream.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/LogInputStream.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.io; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * An InputStream implementation that stores in the bytes it + * reads up to a certain limit. These bytes are then used in + * order to log incoming messages to the RESTEngine. + * + */ +public class LogInputStream extends InputStream { + + private static final Log log = LogFactory.getLog(LogInputStream.class); + + private InputStream is; + + private byte[] bytes; + + private int bytesIndex = 0; + + private int logsize; + + public LogInputStream(InputStream is, int logsize) { + this.is = is; + bytes = new byte[logsize]; + this.logsize = logsize; + } + + @Override + public int read() throws IOException { + if (log.isDebugEnabled()) { + log.debug("read()"); + } + int i = is.read(); + if (bytes != null) { + if (bytesIndex + 1 < logsize) { + bytes[bytesIndex] = (byte) i; + bytesIndex++; + } + } + return i; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (log.isDebugEnabled()) { + log.debug("read offset= " + off + " length= " + len); + } + int bytesRead = is.read(b, off, len); + if (bytes != null) { + int toWrite = bytesRead; + if (toWrite + bytesIndex >= logsize) { + toWrite = logsize - bytesIndex - 1; + } + if (toWrite > 0) { + System.arraycopy(b, off, bytes, bytesIndex, toWrite); + bytesIndex += toWrite; + } + } + return bytesRead; + } + + @Override + public int read(byte[] b) throws IOException { + if (log.isDebugEnabled()) { + log.debug("read byte[].length= " + b.length); + } + int bytesRead = is.read(b); + if (bytes != null) { + int toWrite = bytesRead; + if (toWrite + bytesIndex >= logsize) { + toWrite = logsize - bytesIndex - 1; + } + if (toWrite > 0) { + System.arraycopy(b, 0, bytes, bytesIndex, toWrite); + bytesIndex += toWrite; + } + } + return bytesRead; + } + + public void logBytes() { + log.debug("Request message received:\n" + new String(bytes).trim()); + bytes = null; + } + + @Override + public int available() throws IOException { + return is.available(); + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/LogOutputStream.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/LogOutputStream.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/LogOutputStream.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/io/LogOutputStream.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.io; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * An OutputStream implementation that stores in the bytes it writes up to a + * certain limit. These bytes are then used in order to log outgoing messages + * from the RESTEngine. + * + */ +public class LogOutputStream extends OutputStream { + + private static final Log log = LogFactory.getLog(LogOutputStream.class); + + private OutputStream os; + + private byte[] bytes; + + private int bytesIndex = 0; + + private int logsize; + + public LogOutputStream(OutputStream os, int logsize) { + this.os = os; + bytes = new byte[logsize]; + this.logsize = logsize; + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + if (log.isDebugEnabled()) { + log.debug("write(), offset= " + off + ", length= " + len); + } + if (bytes != null) { + int toWrite = len; + if (toWrite + bytesIndex >= logsize) { + toWrite = logsize - bytesIndex - 1; + } + if (toWrite > 0) { + System.arraycopy(b, off, bytes, bytesIndex, toWrite); + bytesIndex += toWrite; + } + } + os.write(b, off, len); + } + + @Override + public void write(byte[] b) throws IOException { + if (log.isDebugEnabled()) { + log.debug("write() byte[].length= " + b.length); + } + int toWrite = b.length; + if (toWrite + bytesIndex >= logsize) { + toWrite = logsize - bytesIndex - 1; + } + if (toWrite > 0) { + if (bytes != null) { + System.arraycopy(b, 0, bytes, bytesIndex, toWrite); + bytesIndex += toWrite; + } + } + os.write(b); + } + + @Override + public void write(int b) throws IOException { + if (log.isDebugEnabled()) { + log.debug("write()"); + } + if (bytes != null) { + bytes[bytesIndex] = (byte) b; + bytesIndex++; + } + os.write(b); + } + + public void logBytes() { + if (bytes != null) { + log.debug("Response message sent:\n" + new String(bytes).trim()); + } + bytes = null; + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/BaseLifecycleManager.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/BaseLifecycleManager.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/BaseLifecycleManager.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/BaseLifecycleManager.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,182 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.lifecycle; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Type; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Map; + +import javax.ws.rs.WebApplicationException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy; +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.model.ProviderInfo; +import org.apache.cxf.jaxrs.utils.InjectionUtils; +import org.apache.cxf.jaxrs.utils.JAXRSUtils; + +import com.ibm.ws.jaxrs.context.RESTContext; +import com.ibm.ws.jaxrs.i18n.Messages; + +/** + * This is the base class for both the Resource and Provider LifecycleManager + * implementations. Common function, such as selecting an appropriate constructor + * and creating an instance, will be delegated here. + * + */ +public class BaseLifecycleManager implements LifecycleManager { + + private static final Log log = LogFactory + .getLog(BaseLifecycleManager.class); + + /** + * Creates an object instance based on the AbstractResourceInfo. + */ + public Object createInstance(AbstractResourceInfo info, RESTContext context) + throws WebApplicationException { + Object instance = null; + Class clazz = null; + + // if this is a singleton we will just get the supplied instance + if (info.isSingleton()) { + instance = info.getSingleton(); + if (log.isDebugEnabled()) { + log.debug("Retrieved singleton instance for class: " + + instance.getClass().getName()); + } + } else { + if (info instanceof ClassResourceInfo) { + clazz = ((ClassResourceInfo) info).getServiceClass(); + } else if (info instanceof ProviderInfo) { + clazz = ((ProviderInfo) info).getProviderClass(); + } + + if (clazz == null) { + throw new IllegalArgumentException(Messages + .getMessage("noClassFound")); + } + + final Constructor constructor = info + .getSelectedServiceClassConstructor(); + if (constructor == null) { + return null; + } + final Object[] cstrParamInstances = getParamInstances(info, + context, constructor); + try { + instance = AccessController + .doPrivileged(new PrivilegedExceptionAction() { + public Object run() throws IllegalAccessException, + InvocationTargetException, + InstantiationException { + return constructor + .newInstance(cstrParamInstances); + } + }); + } catch (PrivilegedActionException e) { + if (log.isErrorEnabled()) { + log.error("Could not instiantiate class: " + + clazz.getName()); + } + } + if (instance == null) { + return null; + } + if (log.isDebugEnabled()) { + log.debug("Created instance for class: " + + instance.getClass().getName()); + } + } + + try { + /* + * any singletons instianted already has the threadlocal proxies injected into them + */ + + // now inject the context fields and methods + InjectionUtils.injectContextFields(instance, info, context); + InjectionUtils.injectContextMethods(instance, info, context); + + // this is for the Environment injection (Java EE). + // this method does not need to be called for spec compliance. + InjectionUtils.injectEnvironmentFields(instance, info, context); + // there should be another environment injection for the @Resource, etc. methods here + } catch (Exception e) { + if (log.isErrorEnabled()) { + String msg = Messages.getMessage("injectionFail", instance + .getClass().getName()); + log.error(msg, e); + } + if (e instanceof WebApplicationException) { + throw (WebApplicationException) e; + } + throw new WebApplicationException(e, 404); + } + + return instance; + } + + /** + * This method will create and inject the constructor parameters. + * + */ + Object[] getParamInstances(AbstractResourceInfo info, RESTContext context, Constructor cstr) { + try { + Object[] paramInstances = null; + Map, ThreadLocalProxy> cstrProxies = info + .getConstructorContextProxies(cstr); + Class[] paramTypes = cstr.getParameterTypes(); + Annotation[][] allAnnots = cstr.getParameterAnnotations(); + paramInstances = new Object[paramTypes.length]; + int i = 0; + for (Class paramType : paramTypes) { + ThreadLocalProxy proxy = cstrProxies != null ? cstrProxies + .get(paramType) : null; + Type genericType = cstr.getGenericParameterTypes()[i]; + // if it was annotated with @Context it should have a proxy + if (proxy != null) { + proxy.set(JAXRSUtils.createContextValue(context, + genericType, paramType)); + paramInstances[i] = proxy; + } + + // otherwise, we'll have to inject it via one of the value maps + else { + Annotation[] paramAnnots = allAnnots[i]; + paramInstances[i] = InjectionUtils.handleNonContextParam( + paramType, genericType, context, paramAnnots); + } + i++; + } + return paramInstances; + } catch (Exception e) { + throw new WebApplicationException(e); + } + + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleException.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleException.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleException.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleException.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.lifecycle; + +/** + * This is a custom exception class to convey error information that occurs when + * creating instances of classes for the REST runtime. + * + */ +public class LifecycleException extends Exception { + + private static final long serialVersionUID = -4628710328408870824L; + + public LifecycleException() { + super(); + } + + public LifecycleException(String message, Throwable cause) { + super(message, cause); + } + + public LifecycleException(String message) { + super(message); + } + + public LifecycleException(Throwable cause) { + super(cause); + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleManager.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleManager.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleManager.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleManager.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.lifecycle; + +import javax.ws.rs.WebApplicationException; + +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; + +import com.ibm.ws.jaxrs.context.RESTContext; + +/** + * This interface will be implemented by classes that wish to provide instances + * of classes needed by the REST runtime. + * + */ +public interface LifecycleManager { + + /** + * This method will be called to create an instance of an object based on + * some concrete implementation of the AbstractResourceInfo class. + */ + public Object createInstance(AbstractResourceInfo info, RESTContext context) + throws WebApplicationException; + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleManagerFactory.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleManagerFactory.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleManagerFactory.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/LifecycleManagerFactory.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.lifecycle; + +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.model.ProviderInfo; + +import com.ibm.ws.jaxrs.i18n.Messages; + +/** + * This class will be responsible for returning an instance of a + * LifeCycleManager implementation based on the type of AbstractResourceInfo + * that is supplied. + * + */ +public class LifecycleManagerFactory { + + public static LifecycleManager getLifeCycleManager(AbstractResourceInfo info) + throws IllegalArgumentException { + if (info instanceof ProviderInfo) { + return new ProviderLifecycleManager(); + } else if (info instanceof ClassResourceInfo) { + return new ResourceLifecycleManager(); + } else { + throw new IllegalArgumentException(Messages.getMessage( + "invalidInput00", (info != null ? info.getClass().getName() + : ""))); + } + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/ProviderLifecycleManager.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/ProviderLifecycleManager.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/ProviderLifecycleManager.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/ProviderLifecycleManager.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.lifecycle; + +import javax.ws.rs.WebApplicationException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; +import org.apache.cxf.jaxrs.model.ProviderInfo; + +import com.ibm.ws.jaxrs.context.RESTContext; +import com.ibm.ws.jaxrs.i18n.Messages; + +/** + * This class will be used to manage the creation and lifecycle of provider + * classes represented by ProviderInfo instances. + * + */ +public class ProviderLifecycleManager extends BaseLifecycleManager implements LifecycleManager { + + private static final Log log = LogFactory + .getLog(ProviderLifecycleManager.class); + + /** + * This method will create an instance of a class that was annotated with + * the @Provider annotation. If the creation of an instance fails, a + * LifecycleException will be thrown. + * + */ + @Override + public Object createInstance(AbstractResourceInfo abstractInfo, RESTContext context) + throws WebApplicationException, IllegalArgumentException { + + // make sure we can handle the input + if (!(abstractInfo instanceof ProviderInfo)) { + throw new IllegalArgumentException(Messages.getMessage( + "invalidInput00", (abstractInfo != null ? abstractInfo + .getClass().getName() : ""), this.getClass() + .getName())); + } + + ProviderInfo providerInfo = (ProviderInfo) abstractInfo; + final Class providerClass = providerInfo.getProviderClass(); + + if (log.isDebugEnabled()) { + log.debug("Creating JAX-RS Provider instance for the " + + providerClass.getName() + " class"); + } + + Object instance = super.createInstance(abstractInfo, context); + return instance; + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/ResourceLifecycleManager.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/ResourceLifecycleManager.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/ResourceLifecycleManager.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/lifecycle/ResourceLifecycleManager.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.lifecycle; + +import javax.ws.rs.WebApplicationException; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.cxf.jaxrs.model.AbstractResourceInfo; +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.utils.InjectionUtils; + +import com.ibm.ws.jaxrs.context.RESTContext; +import com.ibm.ws.jaxrs.i18n.Messages; + +/** + * This class will be used to manage the creation and lifecycle of resource + * classes represented by ClassResourceInfo instances. + * + */ +public class ResourceLifecycleManager extends BaseLifecycleManager implements LifecycleManager { + + private static final Log log = LogFactory + .getLog(ResourceLifecycleManager.class); + + /** + * This method will create an instance of a class that was annotated with + * the + * + * @Path annotation. If the creation of an instance fails, a + * LifecycleException will be thrown. + * + */ + @Override + public Object createInstance(AbstractResourceInfo abstractInfo, RESTContext context) + throws WebApplicationException, IllegalArgumentException { + + // make sure we can handle the input + if (!(abstractInfo instanceof ClassResourceInfo)) { + throw new IllegalArgumentException(Messages.getMessage( + "invalidInput00", (abstractInfo != null ? abstractInfo + .getClass().getName() : ""), this.getClass() + .getName())); + } + + ClassResourceInfo resourceInfo = (ClassResourceInfo) abstractInfo; + final Class resourceClass = resourceInfo.getServiceClass(); + + if (log.isDebugEnabled()) { + log.debug("Creating JAX-RS Provider instance for the " + + resourceClass.getName() + " class"); + } + Object instance = super.createInstance(abstractInfo, context); + /* + * this inject's the JAX-RS root resource classes with the root resource specific parameters + */ + if(instance == null) { + return null; + } + try { + InjectionUtils.injectResourceBeanFields(instance, abstractInfo, + context); + InjectionUtils.injectResourceBeanMethods(instance, abstractInfo, + context); + } catch (Exception e) { + if (log.isErrorEnabled()) { + String msg = Messages.getMessage("injectionFail", instance + .getClass().getName()); + log.error(msg, e); + } + if (e instanceof WebApplicationException) { + throw (WebApplicationException) e; + } + throw new WebApplicationException(e, 404); + } + + return instance; + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/metadata/MetadataRegistry.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/metadata/MetadataRegistry.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/metadata/MetadataRegistry.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/metadata/MetadataRegistry.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.metadata; + +import java.util.Hashtable; +import java.util.Map; + +import com.ibm.ws.jaxrs.model.JAXRSInfoBuilderFactory; +import com.ibm.ws.jaxrs.model.JAXRSInfoBuilderFactoryImpl; + +/** + * This will be a registry that contains implementations of plug points + * defined within the metadata processing layer. + * + */ +public class MetadataRegistry { + + private static Map, Object> registry = new Hashtable, Object>(); + + static { + registry.put(JAXRSInfoBuilderFactory.class, + new JAXRSInfoBuilderFactoryImpl()); + } + + public static void registerImplementation(Class clazz, Object implementation) { + registry.put(clazz, implementation); + } + + public static Object getImplementation(Class clazz) { + return registry.get(clazz); + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/metadata/RESTMetaData.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/metadata/RESTMetaData.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/metadata/RESTMetaData.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/metadata/RESTMetaData.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.metadata; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.model.ProviderInfo; + +/** + * This class will be a 'holder' for REST metadata needed by the runtime. This + * will allow us to build the metadata once and have it re-used after the + * initial build. This is a singleton. + * + */ +public class RESTMetaData { + + private List classInfoList; + + private List providerInfoList; + + public RESTMetaData() { + classInfoList = new ArrayList(); + providerInfoList = new ArrayList(); + } + + public List getClassInfoList() { + return classInfoList; + } + + public List getProviderInfoList() { + return providerInfoList; + } + + // Only for unit test purposes + void clear() { + classInfoList.clear(); + providerInfoList.clear(); + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/ApplicationProcessor.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/ApplicationProcessor.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/ApplicationProcessor.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/ApplicationProcessor.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,200 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.model; + +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import javax.ws.rs.core.Application; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.ibm.ws.jaxrs.i18n.Messages; +import com.ibm.ws.jaxrs.metadata.MetadataRegistry; +import com.ibm.ws.jaxrs.validation.RESTValidator; + +/** + * This class will be responsible for processing the Application subclass + * provided by RESTful applications. + * + */ +public class ApplicationProcessor { + + private URL configURL; + + private ClassLoader classLoader; + + public ApplicationProcessor(URL configURL, ClassLoader classLoader) { + this.configURL = configURL; + this.classLoader = classLoader; + } + + private static final Log log = LogFactory + .getLog(ApplicationProcessor.class); + + /** + * This method will drive the processing of the supplied Application instance. + * Using the instance, an instance of RESTInfoInput will be built and returned. + * + */ + public JAXRSInfoOutput processApplication(Application application) + throws Exception { + + JAXRSInfoOutput output = null; + if (application != null && configURL != null) { + output = processAll(application, configURL); + } else if (application != null) { + output = processViaReflection(application); + } else { + output = processFromConfigFile(configURL); + } + return output; + } + + /** + * This drives the building of metadata using Java reflection and XML + * configuration. + */ + JAXRSInfoOutput processAll(Application application, URL myConfigURL) + throws Exception { + JAXRSInfoInput input = new JAXRSInfoInput( + JAXRSInfoInput.Type.XML_REFLECTION); + initializeInput(input, application, myConfigURL); + JAXRSInfoBuilderFactory factory = (JAXRSInfoBuilderFactory) MetadataRegistry + .getImplementation(JAXRSInfoBuilderFactory.class); + JAXRSInfoBuilder builder = factory.getInfoBuilder(input.getInputType()); + JAXRSInfoOutput output = builder.buildRESTInfo(input); + + // let's kick off some validation + RESTValidator.validateOutput(output); + + return output; + } + + /** + * This drives the building of metadata using Java reflection to introspect + * on the JAX-RS application components. + */ + JAXRSInfoOutput processViaReflection(Application application) + throws Exception { + JAXRSInfoInput input = new JAXRSInfoInput( + JAXRSInfoInput.Type.REFLECTION); + initializeInput(input, application, null); + + // take the built input and build up the output + JAXRSInfoBuilderFactory factory = (JAXRSInfoBuilderFactory) MetadataRegistry + .getImplementation(JAXRSInfoBuilderFactory.class); + JAXRSInfoBuilder builder = factory.getInfoBuilder(input.getInputType()); + JAXRSInfoOutput output = builder.buildRESTInfo(input); + + // let's kick off some validation + RESTValidator.validateOutput(output); + + return output; + } + + /** + * This method will drive the building of metadata from an ibm-jaxrs.xml + * configuration file. + */ + JAXRSInfoOutput processFromConfigFile(URL configFileURL) throws Exception { + JAXRSInfoOutput output = null; + JAXRSInfoInput input = new JAXRSInfoInput(JAXRSInfoInput.Type.XMLCONFIG); + initializeInput(input, null, configURL); + JAXRSInfoBuilderFactory factory = (JAXRSInfoBuilderFactory) MetadataRegistry + .getImplementation(JAXRSInfoBuilderFactory.class); + JAXRSInfoBuilder builder = factory.getInfoBuilder(input.getInputType()); + output = builder.buildRESTInfo(input); + RESTValidator.validateOutput(output); + return output; + } + + /** + * Initializes JAXRSInfoInput instance based on an Application instance. + */ + void initializeInput(JAXRSInfoInput input, Application application, URL cfgURL) + throws Exception { + // process the Application instance + if (application != null) { + // first process the singleton set + Set singletons = application.getSingletons(); + List singletonNames = new ArrayList( + singletons != null ? singletons.size() : 0); + + if (singletons != null && !singletons.isEmpty()) { + Iterator singletonIter = singletons.iterator(); + while (singletonIter.hasNext()) { + Object singleton = singletonIter.next(); + String key = singleton.getClass().getName(); + + // if an instance of the same class has already been processed + // we should throw an exception + if (singletonNames.contains(key)) { + throw new RuntimeException(Messages.getMessage( + "multipleResourceClasses00", key, application + .getClass().getName())); + } + if (log.isDebugEnabled()) { + log + .debug("Adding singleton instance for class: " + + key); + } + + input.addInfo(key, singleton.getClass(), singleton); + singletonNames.add(key); + } + } + + // now process the class set + Set> classes = application.getClasses(); + if (classes != null && !classes.isEmpty()) { + Iterator> classIter = classes.iterator(); + while (classIter.hasNext()) { + Class clazz = classIter.next(); + String key = clazz.getName(); + + // make sure this class was not in the singleton list + if (singletonNames.contains(key)) { + log.warn(Messages.getMessage( + "multipleResourceClasses01", key)); + } else { + if (log.isDebugEnabled()) { + log.debug("Adding class: " + key); + } + input.addInfo(key, clazz, null); + } + } + } + } + + // handle the URL for the config file + if (cfgURL != null) { + File configFile = new File(cfgURL.toURI()); + input.setClassLoader(classLoader); + input.setConfigFile(configFile); + } + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfo.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfo.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfo.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfo.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.model; + +/** + * This bean will hold information about a resource or provider class. + * + */ +public class JAXRSInfo { + + // This should always be non-null + private Class clazz; + + // This will only be non-null if it is a singleton + private Object instance; + + public JAXRSInfo(Class clazz, Object instance) { + this.clazz = clazz; + this.instance = instance; + } + + public Class getRESTClass() { + return clazz; + } + + public Object getInstance() { + return instance; + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilder.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilder.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilder.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilder.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.model; + +/** + * This interface will be implemented by classes that build up a data model for + * a collection of JAX-RS resource objects. + * + */ +public interface JAXRSInfoBuilder { + + /** + * This method will be called to build up a list of ClassResourceInfo + * objects from the supplied ResoureBuilderSource input. Each + * ClassResourceInfo will represent an annotated JAX-RS + * resource/sub-resource. + */ + public JAXRSInfoOutput buildRESTInfo(JAXRSInfoInput source) + throws Exception; + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilderFactory.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilderFactory.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilderFactory.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilderFactory.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.model; + +/** + * Interface that will be implemented by factories that return a + * JAXRSInfoBuilder implementation based on the JAXRSInfoInput.Type. + * + */ +public interface JAXRSInfoBuilderFactory { + + /** + * This method will return a JAXRSInfoBuilder based on input type. + */ + public JAXRSInfoBuilder getInfoBuilder(JAXRSInfoInput.Type inputType); + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilderFactoryImpl.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilderFactoryImpl.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilderFactoryImpl.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoBuilderFactoryImpl.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.model; + +import com.ibm.ws.jaxrs.i18n.Messages; +import com.ibm.ws.jaxrs.model.JAXRSInfoInput.Type; + +/** + * Interface that will be implemented by factories that return a + * JAXRSInfoBuilder implementation based on the JAXRSInfoInput.Type. + * + */ +public class JAXRSInfoBuilderFactoryImpl implements JAXRSInfoBuilderFactory { + + /** + * This method will return a JAXRSInfoBuilder based on input type. + */ + public JAXRSInfoBuilder getInfoBuilder(JAXRSInfoInput.Type inputType) + throws IllegalArgumentException { + if (Type.REFLECTION.equals(inputType)) { + return new ReflectiveJAXRSInfoBuilder(); + } else if (Type.XMLCONFIG.equals(inputType)) { + return new XMLJAXRSInfoBuilder(); + } else if (Type.XML_REFLECTION.equals(inputType)) { + return new XMLReflectionInfoBuilder(); + } else { + throw new IllegalArgumentException(Messages.getMessage( + ("invalidInput00"), inputType == null ? "" + : inputType.getClass().getName())); + } + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoInput.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoInput.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoInput.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoInput.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.model; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +/** + * This class will be used to hold input for RESTInfoBuilder implementations. + * The input may be of various forms, and it is up to the RESTInfoBuilder + * implementation to interpret and process the input. + * + */ +public class JAXRSInfoInput { + + protected Type inputType; + + public JAXRSInfoInput(Type inputType) { + this.inputType = inputType; + } + + /** + * An enumeration of the various different types of input. + * + */ + public enum Type { + // input is a collection of Class objects + REFLECTION, + + // input is an XML file describing the config + XMLCONFIG, + + // input contains both Class objects and XML config + XML_REFLECTION, + + } + + // This is a map of fully qualified class name to RESTInfo + protected Map restInfoMap; + + // The File object representing an XML configuration file + private File configFile; + + private ClassLoader classLoader; + + public void setClasses(Map> classMap) { + if (classMap != null && !classMap.isEmpty()) { + for (Class clazz : classMap.values()) { + addInfo(clazz.getName(), clazz, null); + } + } + } + + public void setSingletons(Map singletons) { + if (singletons != null && !singletons.isEmpty()) { + for (Object singleton : singletons.values()) { + Class singletonClass = singleton.getClass(); + addInfo(singletonClass.getName(), singletonClass, singleton); + } + } + } + + public void addInfo(String key, Class clazz, Object singleton) { + if (restInfoMap == null) { + restInfoMap = new HashMap(); + } + JAXRSInfo info = new JAXRSInfo(clazz, singleton); + restInfoMap.put(key, info); + } + + public void setConfigFile(File configFile) { + this.configFile = configFile; + } + + public File getConfigFile() { + return configFile; + } + + public Map getInfos() { + return restInfoMap; + } + + public Type getInputType() { + return inputType; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + public void setClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + +} Added: incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoOutput.java URL: http://svn.apache.org/viewvc/incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoOutput.java?rev=787557&view=auto ============================================================================== --- incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoOutput.java (added) +++ incubator/wink/contrib/ibm-jaxrs/src/com/ibm/ws/jaxrs/model/JAXRSInfoOutput.java Tue Jun 23 05:41:49 2009 @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.ibm.ws.jaxrs.model; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.cxf.jaxrs.model.ClassResourceInfo; +import org.apache.cxf.jaxrs.model.ProviderInfo; + +/** + * This class will be used to hold output from RESTInfoBuilder implementations. + * The output will contain information about the resources and providers that + * make up a JAX-RS application. + * + */ +public class JAXRSInfoOutput { + + private List classInfoList; + + private Map classMapping; + + private List providerInfoList; + + private Map providerMapping; + + public JAXRSInfoOutput() { + classMapping = new HashMap(); + providerMapping = new HashMap(); + } + + public void setClassInfoList(List classInfoList) { + this.classInfoList = classInfoList; + if (classInfoList != null) { + for (ClassResourceInfo info : classInfoList) { + classMapping.put(info.getResourceClass().getName(), info); + } + } + } + + public List getClassInfoList() { + return classInfoList; + } + + public void setProviderInfoList(List providerInfoList) { + this.providerInfoList = providerInfoList; + if (providerInfoList != null) { + for (ProviderInfo info : providerInfoList) { + providerMapping.put(info.getProviderClass().getName(), info); + } + } + } + + public List getProviderInfoList() { + return providerInfoList; + } + + public ClassResourceInfo getClassResourceInfo(String className) { + return classMapping.get(className); + } + + public ProviderInfo getProviderInfo(String className) { + return providerMapping.get(className); + } + +}