Skip to content

Instantly share code, notes, and snippets.

@aslakknutsen
Created June 14, 2011 19:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aslakknutsen/1025597 to your computer and use it in GitHub Desktop.
A non hierarchy take on Descriptors
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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 org.jboss.shrinkwrap.descriptor.api;
import static org.jboss.shrinkwrap.descriptor.api.APITestCase.Descriptors.create;
import static org.jboss.shrinkwrap.descriptor.api.APITestCase.Descriptors.type;
import java.util.Collection;
import org.junit.Test;
/**
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
public class APITestCase
{
public class MyServlet {}
public class MyFilter {}
@Test
public void api() throws Exception
{
WebAppDescriptor desc = create(WebAppDescriptor.class)
.add(type(Servlet.class)
.className(MyServlet.class)
.add(type(Mapping.class)
.url("/MyEntryPoint"))
)
.add(type(Filter.class)
.name("MyFilter")
.className("com.acme.LogFilter")
.add(type(Mapping.class)
.url("/*"))
)
.add(type(Filter.class)
.className(MyFilter.class)
.add(type(Mapping.class)
.url("/*")));
for(ServletReader servlet : desc.get(Servlet.class))
{
System.out.println("Servlet: " + servlet.name());
for(MappingReader mapping : servlet.mappings())
{
System.out.println("Mapped to: " + mapping.urls());
}
}
}
public interface DescriptorReader
{
}
public interface Descriptor
{
}
public interface TypeReader
{
}
public interface Type
{
}
public interface WebAppTypeReader<T extends TypeReader> extends TypeReader
{
}
public interface WebAppType extends Type
{
}
public interface MappingReader extends TypeReader
{
Collection<String> urls();
}
public interface Mapping extends Type
{
Mapping url(String... urls);
}
public interface ClassTypeReader extends TypeReader
{
String name();
String className();
}
public interface ClassType<T> extends Type
{
T name(String name);
T className(String className);
T className(Class<?> clazz);
}
public interface MappedReader extends TypeReader
{
Collection<MappingReader> mappings();
}
public interface Mapped<T> extends Type
{
T add(Mapping mapping);
}
public interface ServletReader extends ClassTypeReader, MappedReader
{
}
public interface Servlet extends WebAppType, WebAppTypeReader<ServletReader>, ClassType<Servlet>, Mapped<Servlet>
{
}
public interface FilterReader extends ClassTypeReader, MappedReader
{
}
public interface Filter extends WebAppType, WebAppTypeReader<FilterReader>, ClassType<Filter>, Mapped<Filter>
{
}
public interface WebAppDescriptorReader extends Descriptor
{
<X extends TypeReader, T extends WebAppTypeReader<X>> Collection<X> get(Class<T> type);
}
public interface WebAppDescriptor extends WebAppDescriptorReader, Descriptor
{
WebAppDescriptor add(WebAppType type);
}
public static class Descriptors
{
public static <T extends Descriptor> T create(Class<T> type)
{
return null;
}
public static <T extends Type> T type(Class<T> type)
{
return null;
}
}
}
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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 org.jboss.shrinkwrap.descriptor.api;
import static org.jboss.shrinkwrap.descriptor.api.TestCase.Descriptors.build;
import static org.jboss.shrinkwrap.descriptor.api.TestCase.Descriptors.read;
import static org.jboss.shrinkwrap.descriptor.api.TestCase.Descriptors.create;
import static org.jboss.shrinkwrap.descriptor.api.TestCase.Descriptors.type;
import java.security.KeyStore.Builder;
import java.util.Collection;
import org.junit.Test;
/**
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
public class TestCase
{
public abstract class MyServlet implements Servlet { }
@Test
public void shouldBeAbleTo()
{
WebAppDescriptor desc = create(WebAppDescriptor.class)
.add(type(Servlet.class)
.className("com.acme.MyServlet")
.name("MyServletName"))
.add(build(Servlet.class)
.from(MyServlet.class));
Collection<ServletReader> servlets = desc.get(Servlet.class);
for(ServletReader servlet : servlets)
{
System.out.println(servlet.className());
}
}
public interface Descriptor
{
}
public interface TypeReader
{
}
public interface TypeBuilder
{
}
public interface Type<READER extends TypeReader, BUILDER extends TypeBuilder>
{
}
public interface SubTypeReader<X extends TypeReader, S extends Type<X, ?>>
{
Collection<? extends X> get(Class<? extends S> type);
}
public interface SubType<R, S extends Type<?, ?>>
{
//<X extends TypeReader, T extends Type<X, ?>> Collection<X> get(Class<? extends T> type);
R add(S type);
}
public interface WebAppDescriptor extends
Descriptor,
SubType<WebAppDescriptor, WebAppType<? extends TypeReader, ? extends TypeBuilder>>,
SubTypeReader<WebAppTypeReader, WebAppType<WebAppTypeReader, WebAppTypeBuilder>>
{
}
public interface WebAppTypeReader extends TypeReader
{
}
public interface WebAppTypeBuilder extends TypeBuilder
{
}
public interface WebAppType<READER extends WebAppTypeReader, BUILDER extends WebAppTypeBuilder> extends Type<READER, BUILDER>
{
}
public interface ServletBuilder extends WebAppTypeBuilder
{
Servlet from(Class<? extends Servlet> servlet);
}
public interface ServletReader extends WebAppTypeReader
{
String name();
String className();
}
public interface Servlet extends WebAppType<ServletReader, ServletBuilder>, ServletReader
{
Servlet name(String name);
Servlet className(String className);
}
@Test
public void should()
{
build(Servlet.class)
.from(TestCase.class);
type(Servlet.class)
.className("")
.className();
read(Servlet.class)
.className();
}
public static class Descriptors
{
public static <T extends Descriptor> T create(Class<T> type)
{
return null;
}
public static <T extends Type<?, ?>> T type(Class<T> type)
{
return null;
}
public static <X extends TypeBuilder, T extends Type<?, X>> X build(Class<T> type)
{
return null;
}
public static <X extends TypeReader, T extends Type<X, ?>> X read(Class<T> type)
{
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment