Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrinkdesc 38 - Descriptor.getDescriptorName #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
*/
public interface Descriptor
{
/**
* Get the Descriptor name. The name can be specified by user or predefined.
*
* @return the descriptor name
*/
String getDescriptorName();

/**
* Exports the descriptor XML as a {@link String}
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class DescriptorConstructionInfo
*/
final Class<? extends DescriptorImporter<?>> importerClass;

/**
* The default name of the Descriptor
*/
final String defaultName;

//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
Expand All @@ -50,9 +55,10 @@ class DescriptorConstructionInfo
* Creates a new instance using the specified
* @param implClassName
* @param modelClassName
* @param defaultName The default name for this Descriptor
*/
@SuppressWarnings("unchecked")
DescriptorConstructionInfo(final String implClassName, String importerClassName)
DescriptorConstructionInfo(final String implClassName, String importerClassName, String defaultName)
{
// Get the TCCL
final ClassLoader tccl = AccessController.doPrivileged(GetTcclAction.INSTANCE);
Expand Down Expand Up @@ -85,5 +91,6 @@ class DescriptorConstructionInfo
}

this.importerClass = importerClass;
this.defaultName = defaultName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class DescriptorInstantiator
*/
private static final String KEY_IMPL_CLASS_NAME = "implClass";

/**
* Key of the property denoting the default descriptor name
*/
private static final String KEY_DEFAULT_NAME = "defaultName";

/**
* Key of the property denoting the backing model class for a given end-user view type
*/
Expand Down Expand Up @@ -77,30 +82,37 @@ private DescriptorInstantiator()
*
* @param <T>
* @param userViewClass
* @param descriptorName The name of the descriptor. If argument is null, the default name will be used.
* @return
* @throws IllegalArgumentException If the user view class was not specified
*/
static <T extends Descriptor> T createFromUserView(final Class<T> userViewClass) throws IllegalArgumentException
static <T extends Descriptor> T createFromUserView(final Class<T> userViewClass, String descriptorName) throws IllegalArgumentException
{
// Get the construction information
final DescriptorConstructionInfo info = getDescriptorConstructionInfoForUserView(userViewClass);

// Get the constructor to use in making the new instance
final Constructor<? extends Descriptor> ctor;
try
{
ctor = info.implClass.getConstructor();
ctor = info.implClass.getConstructor(String.class);
}
catch (final NoSuchMethodException nsme)
{
throw new RuntimeException(info.implClass + " must contain a constructor no args contructor");
throw new RuntimeException(info.implClass + " must contain a constructor with a single String argument");
}

String name = info.defaultName;
if(descriptorName != null)
{
name = descriptorName;
}

// Create a new descriptor instance using the backing model
final Descriptor descriptor;
try
{
descriptor = ctor.newInstance();
descriptor = ctor.newInstance(name);
}
// Handle all construction errors equally
catch (final Exception e)
Expand All @@ -124,11 +136,12 @@ static <T extends Descriptor> T createFromUserView(final Class<T> userViewClass)
*
* @param <T>
* @param userViewClass
* @param descriptorName The name of the descriptor. If argument is null, the default name will be used.
* @return
* @throws IllegalArgumentException If the user view class was not specified
*/
@SuppressWarnings("unchecked")
static <T extends Descriptor> DescriptorImporter<T> createImporterFromUserView(final Class<T> userViewClass)
static <T extends Descriptor> DescriptorImporter<T> createImporterFromUserView(final Class<T> userViewClass, String descriptorName)
throws IllegalArgumentException
{
// Get the construction information
Expand All @@ -143,10 +156,16 @@ static <T extends Descriptor> DescriptorImporter<T> createImporterFromUserView(f
}
final Class<T> implClassCasted = (Class<T>) implClass;

String name = info.defaultName;
if(descriptorName != null)
{
name = descriptorName;
}

final DescriptorImporter<T> importer;
try
{
importer = (DescriptorImporter<T>)info.importerClass.getConstructor(Class.class).newInstance(implClassCasted);
importer = (DescriptorImporter<T>)info.importerClass.getConstructor(Class.class, String.class).newInstance(implClassCasted, name);
}
catch (Exception e)
{
Expand Down Expand Up @@ -214,9 +233,16 @@ private static DescriptorConstructionInfo getDescriptorConstructionInfoForUserVi
throw new IllegalStateException("Resource " + resourceName + " for " + userViewClass
+ " does not contain key " + KEY_IMPORTER_CLASS_NAME);
}

final String defaultName = props.getProperty(KEY_DEFAULT_NAME);
if(defaultName == null)
{
throw new IllegalStateException("Resource " + resourceName + " for " + userViewClass
+ " does not contain key " + KEY_DEFAULT_NAME);
}

// Get the construction information
final DescriptorConstructionInfo info = new DescriptorConstructionInfo(implClassName, importerClassName);
final DescriptorConstructionInfo info = new DescriptorConstructionInfo(implClassName, importerClassName, defaultName);

// Return
return info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,39 @@ private Descriptors()
}

/**
* Creates a new Descriptor instance
* Creates a new Descriptor instance, predefined default descriptor name will be used.
*
* @param <T>
* @param type
* @return
* @see #create(Class, String)
*/
public static <T extends Descriptor> T create(final Class<T> type)
{
return DescriptorInstantiator.createFromUserView(type);
return create(type, null);
}

/**
* Creates a new named Descriptor instance.
*
* @param <T>
* @param type
* @param descriptorName the descriptor name
* @return
*/
public static <T extends Descriptor> T create(final Class<T> type, String descriptorName)
{
return DescriptorInstantiator.createFromUserView(type, descriptorName);
}

public static <T extends Descriptor> DescriptorImporter<T> importAs(final Class<T> type)
{
return DescriptorInstantiator.createImporterFromUserView(type);
return importAs(type, null);
}

public static <T extends Descriptor> DescriptorImporter<T> importAs(final Class<T> type, String descriptorName)
{
return DescriptorInstantiator.createImporterFromUserView(type, descriptorName);
}

// public static <T extends Descriptor, X extends Descriptor<T>> X create(Class<X> defType, InputStream xmlStream)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.impl.base;

import org.jboss.shrinkwrap.descriptor.api.Descriptor;

/**
* Base implementation for a Descriptor.
*
* Enforces descriptor name constructor argument contract from extension loading.
*
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
public abstract class DescriptorImplBase implements Descriptor
{
private final String name;

/**
* Create a named Descriptor.
*/
public DescriptorImplBase(String name)
{
this.name = name;
}

/* (non-Javadoc)
* @see org.jboss.shrinkwrap.descriptor.api.Descriptor#getName()
*/
@Override
public String getDescriptorName()
{
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public abstract class DescriptorImporterBase<T extends Descriptor> implements De
* the model during construction)
*/
private final Class<T> endUserViewImplType;


private final String descriptorName;

//-------------------------------------------------------------------------------------||
// Constructor ------------------------------------------------------------------------||
//-------------------------------------------------------------------------------------||
Expand All @@ -56,18 +58,25 @@ public abstract class DescriptorImporterBase<T extends Descriptor> implements De
*
* @param The type of the backing object model for the descriptor
* @throws IllegalArgumentException If the model type is not specified
* @throws IllegalArgumentException If the descriptorName not specified
*/
public DescriptorImporterBase(final Class<T> endUserViewImplType)
public DescriptorImporterBase(final Class<T> endUserViewImplType, String descriptorName)
throws IllegalArgumentException
{
// Precondition checks
if (endUserViewImplType == null)
{
throw new IllegalArgumentException("End user view impl type must be specified");
}
if (descriptorName == null)
{
throw new IllegalArgumentException("Descriptor name must be specified");
}

// Set

this.endUserViewImplType = endUserViewImplType;
this.descriptorName = descriptorName;
}

@Override
Expand Down Expand Up @@ -127,17 +136,17 @@ public T from(final InputStream in) throws IllegalArgumentException, DescriptorI
final Constructor<T> constructor;
try
{
constructor = endUserViewImplType.getConstructor(Node.class);
constructor = endUserViewImplType.getConstructor(String.class, Node.class);
}
catch (final NoSuchMethodException e)
{
throw new DescriptorImportException("Descriptor impl " + endUserViewImplType.getName()
+ " does not have a constructor accepting " + Node.class.getName(), e);
+ " does not have a constructor accepting " + String.class.getName() + " and " + Node.class.getName(), e);
}
final T descriptor;
try
{
descriptor = constructor.newInstance(rootNode);
descriptor = constructor.newInstance(descriptorName, rootNode);
}
catch (final Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @version $Revision: $
*/
public abstract class NodeProviderImplBase implements NodeProvider
public abstract class NodeProviderImplBase extends DescriptorImplBase implements NodeProvider
{

public NodeProviderImplBase(String name)
{
super(name);
}

/* (non-Javadoc)
* @see org.jboss.shrinkwrap.descriptor.api.Descriptor#exportAsString()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
*/
public class XMLImporter<T extends Descriptor> extends DescriptorImporterBase<T>
{
public XMLImporter(final Class<T> endUserViewImplType)
public XMLImporter(final Class<T> endUserViewImplType, String descriptorName)
{
super(endUserViewImplType);
super(endUserViewImplType, descriptorName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ public class BeansDescriptorImpl extends NodeProviderImplBase implements BeansDe
// Constructor ------------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||

public BeansDescriptorImpl()
public BeansDescriptorImpl(String descriptorName)
{
this(new Node("beans")
this(descriptorName, new Node("beans")
.attribute("xmlns", "http://java.sun.com/xml/ns/javaee")
.attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"));
}

public BeansDescriptorImpl(Node beans)
public BeansDescriptorImpl(String descriptorName, Node beans)
{
super(descriptorName);
this.beans = beans;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ public class ApplicationDescriptorImpl extends NodeProviderImplBase implements A
// Constructor ------------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||

public ApplicationDescriptorImpl()
public ApplicationDescriptorImpl(String descriptorName)
{
this(new Node("application")
this(descriptorName, new Node("application")
.attribute("xmlns", "http://java.sun.com/xml/ns/javaee")
.attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"));

version("6");
}

public ApplicationDescriptorImpl(Node model)
public ApplicationDescriptorImpl(String descriptorName, Node model)
{
super(descriptorName);
this.model = model;
}

Expand Down
Loading