Rethinking Decorators
This commit is contained in:
parent
b7d719be5f
commit
2b7b5ef495
|
@ -1449,11 +1449,12 @@ public class ServletContextHandler extends ContextHandler
|
|||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Interface to decorate loaded classes.
|
||||
/**
|
||||
* Interface to decorate loaded classes.
|
||||
* <p>
|
||||
* Left for backwards compatibility with Weld / CDI
|
||||
*/
|
||||
public interface Decorator
|
||||
public interface Decorator extends org.eclipse.jetty.util.Decorator
|
||||
{
|
||||
<T> T decorate (T o);
|
||||
void destroy (Object o);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.util;
|
||||
|
||||
/**
|
||||
* Interface for 3rd party libraries to decorate recently created objects in Jetty.
|
||||
* <p>
|
||||
* Most common use is weld/CDI.
|
||||
* <p>
|
||||
* This was moved from org.eclipse.jetty.servlet.ServletContextHandler to allow
|
||||
* client applications to also use Weld/CDI to decorate objects.
|
||||
* Such as websocket client (which has no servlet api requirement)
|
||||
*/
|
||||
public interface Decorator
|
||||
{
|
||||
<T> T decorate(T o);
|
||||
|
||||
void destroy(Object o);
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a collection of {@link Decorator} instances that apply to a known
|
||||
* state, such as a WebAppContext, WebSocketServerFactory, or WebSocketClient.
|
||||
* <p>
|
||||
* Consistent single location for all Decorator behavior, with equal behavior in
|
||||
* a ServletContext and also for a stand alone client.
|
||||
*/
|
||||
public class Decorators
|
||||
{
|
||||
private List<Decorator> decorators = new ArrayList<>();
|
||||
|
||||
public List<Decorator> getDecorators()
|
||||
{
|
||||
return Collections.unmodifiableList(decorators);
|
||||
}
|
||||
|
||||
public void setDecorators(List<Decorator> decorators)
|
||||
{
|
||||
this.decorators.clear();
|
||||
if (decorators != null)
|
||||
{
|
||||
this.decorators.addAll(decorators);
|
||||
}
|
||||
}
|
||||
|
||||
public void addDecorator(Decorator decorator)
|
||||
{
|
||||
this.decorators.add(decorator);
|
||||
}
|
||||
|
||||
public void destroy(Object obj)
|
||||
{
|
||||
for (Decorator decorator : this.decorators)
|
||||
{
|
||||
decorator.destroy(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T decorate(T obj)
|
||||
{
|
||||
T f = obj;
|
||||
for (Decorator decorator : this.decorators)
|
||||
{
|
||||
f = decorator.decorate(f);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public <T> T createInstance(Class<T> clazz) throws Exception
|
||||
{
|
||||
T o = clazz.newInstance();
|
||||
return o;
|
||||
}
|
||||
}
|
|
@ -30,6 +30,7 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
@ -38,6 +39,8 @@ import org.eclipse.jetty.io.ByteBufferPool;
|
|||
import org.eclipse.jetty.io.EndPoint;
|
||||
import org.eclipse.jetty.io.MappedByteBufferPool;
|
||||
import org.eclipse.jetty.server.HttpConnection;
|
||||
import org.eclipse.jetty.util.Decorator;
|
||||
import org.eclipse.jetty.util.Decorators;
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
@ -85,6 +88,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
private Set<WebSocketSession> openSessions = new CopyOnWriteArraySet<>();
|
||||
private WebSocketCreator creator;
|
||||
private List<Class<?>> registeredSocketClasses;
|
||||
private Decorators decorators = new Decorators();
|
||||
|
||||
public WebSocketServerFactory()
|
||||
{
|
||||
|
@ -317,6 +321,16 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
{
|
||||
return extensionFactory;
|
||||
}
|
||||
|
||||
public void addDecorator(Decorator decorator)
|
||||
{
|
||||
decorators.addDecorator(decorator);
|
||||
}
|
||||
|
||||
public List<Decorator> getDecorators()
|
||||
{
|
||||
return decorators.getDecorators();
|
||||
}
|
||||
|
||||
public Set<WebSocketSession> getOpenSessions()
|
||||
{
|
||||
|
@ -431,6 +445,11 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
{
|
||||
this.creator = creator;
|
||||
}
|
||||
|
||||
public void setDecorators(List<Decorator> decorators)
|
||||
{
|
||||
this.decorators.setDecorators(decorators);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrade the request/response to a WebSocket Connection.
|
||||
|
|
Loading…
Reference in New Issue