Skip to content

Commit

Permalink
Issue #5020 Reimplement after review
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <[email protected]>
  • Loading branch information
janbartel committed Jul 8, 2020
1 parent 3a5db40 commit e3a4187
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ public ServletHandler()
{
}

@Override
public boolean isDumpable(Object o)
{
if (o instanceof Holder || o instanceof BaseHolder || o instanceof FilterMapping || o instanceof ServletMapping)
return false;
return true;
}

@Override
public void dump(Appendable out, String indent) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public synchronized void clearAttributes()
public void dump(Appendable out, String indent) throws IOException
{
Dumpable.dumpObject(out, this);
Dumpable.dumpMapEntries(out, indent, _map, Collections.emptyList());
Dumpable.dumpMapEntries(out, indent, _map, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
* </pre>
*/
@ManagedObject("Implementation of Container and LifeCycle")
public class ContainerLifeCycle extends AbstractLifeCycle implements Container, Destroyable, Dumpable
public class ContainerLifeCycle extends AbstractLifeCycle implements Container, Destroyable, DumpableContainer
{
private static final Logger LOG = Log.getLogger(ContainerLifeCycle.class);
private final List<Bean> _beans = new CopyOnWriteArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

Expand Down Expand Up @@ -147,34 +145,35 @@ else if (o instanceof Map)
static void dumpObjects(Appendable out, String indent, Object object, Object... extraChildren) throws IOException
{
dumpObject(out, object);
List<Object> list = extraChildren == null ? Collections.emptyList() : Arrays.asList(extraChildren);


int extras = extraChildren == null ? 0 : extraChildren.length;

if (object instanceof Stream)
object = ((Stream)object).toArray();
if (object instanceof Array)
object = Arrays.asList((Object[])object);

if (object instanceof Container)
{
dumpContainer(out, indent, (Container)object, list);
dumpContainer(out, indent, (Container)object, extras == 0);
}
if (object instanceof Iterable)
{
dumpIterable(out, indent, (Iterable<?>)object, list);
dumpIterable(out, indent, (Iterable<?>)object, extras == 0);
}
else if (object instanceof Map)
{
dumpMapEntries(out, indent, (Map<?, ?>)object, list);
dumpMapEntries(out, indent, (Map<?, ?>)object, extras == 0);
}

if (list.size() == 0)
if (extras == 0)
return;

int i = 0;
for (Object item : extraChildren)
{
i++;
String nextIndent = indent + (i < list.size() ? "| " : " ");
String nextIndent = indent + (i < extras ? "| " : " ");
out.append(indent).append("+> ");
if (item instanceof Dumpable)
((Dumpable)item).dump(out, nextIndent);
Expand All @@ -183,24 +182,18 @@ else if (object instanceof Map)
}
}

@Deprecated
static void dumpContainer(Appendable out, String indent, Container object, boolean last) throws IOException
{
dumpContainer(out, indent, object, (last ? Collections.emptyList() : Collections.singletonList(new Object())));
}

static void dumpContainer(Appendable out, String indent, Container object, List<Object> extras) throws IOException
{
Container container = object;
ContainerLifeCycle containerLifeCycle = container instanceof ContainerLifeCycle ? (ContainerLifeCycle)container : null;
for (Iterator<Object> i = container.getBeans().iterator(); i.hasNext(); )
{
Object bean = i.next();

if (isContainedIn(bean, extras))
continue; //will be explictly formatted by caller
if (container instanceof DumpableContainer && !((DumpableContainer)container).isDumpable(bean))
continue; //won't be dumped as a child bean

String nextIndent = indent + ((i.hasNext() || !extras.isEmpty()) ? "| " : " ");
String nextIndent = indent + ((i.hasNext() || !last) ? "| " : " ");
if (bean instanceof LifeCycle)
{
if (container.isManaged(bean))
Expand Down Expand Up @@ -241,63 +234,26 @@ else if (containerLifeCycle != null && containerLifeCycle.isUnmanaged(bean))
}
}

static boolean isContainedIn(Object object, List<Object> objects)
{
if (object == null)
return false;
if (objects == null)
return false;

if (objects.contains(object))
return true;

for (Object o : objects)
{
if (o instanceof DumpableCollection && ((DumpableCollection)o).contains(object))
{
return true;
}
}
return false;
}

@Deprecated
static void dumpIterable(Appendable out, String indent, Iterable<?> iterable, boolean last) throws IOException
{
dumpIterable(out, indent, iterable, last ? Collections.emptyList() : Collections.singletonList(new Object()));
}

static void dumpIterable(Appendable out, String indent, Iterable<?> iterable, List<Object> extras) throws IOException
{
for (Iterator i = iterable.iterator(); i.hasNext(); )
{
Object item = i.next();
if (isContainedIn(item, extras))
continue; //the item will be formatted explicitly by caller

String nextIndent = indent + ((i.hasNext() || !extras.isEmpty()) ? "| " : " ");
String nextIndent = indent + ((i.hasNext() || !last) ? "| " : " ");
out.append(indent).append("+: ");
if (item instanceof Dumpable)
((Dumpable)item).dump(out, nextIndent);
else
dumpObjects(out, nextIndent, item);
}
}

@Deprecated

static void dumpMapEntries(Appendable out, String indent, Map<?, ?> map, boolean last) throws IOException
{
dumpMapEntries(out, indent, map, last ? Collections.emptyList() : Collections.singletonList(new Object()));
}

static void dumpMapEntries(Appendable out, String indent, Map<?, ?> map, List<Object> extras) throws IOException
{
for (Iterator<? extends Map.Entry<?, ?>> i = map.entrySet().iterator(); i.hasNext(); )
{
Map.Entry entry = i.next();
if (isContainedIn(entry, extras))
continue; //will be explicitly formatted by caller
String nextIndent = indent + ((i.hasNext() || !extras.isEmpty()) ? "| " : " ");
String nextIndent = indent + ((i.hasNext() || !last) ? "| " : " ");
out.append(indent).append("+@ ").append(String.valueOf(entry.getKey())).append(" = ");
Object item = entry.getValue();
if (item instanceof Dumpable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public static DumpableCollection from(String name, Object... items)
{
return new DumpableCollection(name, items == null ? Collections.emptyList() : Arrays.asList(items));
}

public boolean contains(Object o)
{
return _collection.contains(o);
}

@Override
public String dump()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// 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.component;

/**
* DumpableContainer
*
* A Dumpable that is a container of beans can implement this
* interface to allow it to refine which of its beans can be
*
*/
public interface DumpableContainer extends Dumpable
{

default boolean isDumpable(Object o)
{
return true;
}

}

0 comments on commit e3a4187

Please sign in to comment.