-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from gastaldi/FURNACE-74
FURNACE-74: SimpleContainer now supports Producers
- Loading branch information
Showing
10 changed files
with
395 additions
and
21 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
api/src/main/java/org/jboss/forge/furnace/container/simple/Producer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2014 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.furnace.container.simple; | ||
|
||
/** | ||
* Interface used to register a Service to be a producer. | ||
* | ||
* <p> | ||
* To register a {@link Producer}, register it as a {@link Service} or {@link SingletonService}. | ||
* <p> | ||
* | ||
* <pre> | ||
* public class ExampleService extends ProducerService<MyObject> | ||
* { | ||
* @Override | ||
* public MyObject get() | ||
* { | ||
* // r MyObject | ||
* } | ||
* } | ||
* </pre> | ||
* <p> | ||
* Example registration file: | ||
* </p> | ||
* | ||
* <pre> | ||
* META-INF/services/org.jboss.forge.furnace.container.simple.Service | ||
* ------- | ||
* org.example.ExampleService | ||
* org.example.ExampleService2 | ||
* org.my.custom.MyService | ||
* ------- | ||
* </pre> | ||
* | ||
* | ||
* <h2>Warning: from JDK 1.8.0_20 onwards, javac changed its behavior in order to check for transitive references. | ||
* Producer services must NEVER be directly accessed outside of the addon in which they are defined. See | ||
* <a href="https://issues.jboss.org/browse/FORGE-2019">this link</a> for more information</h2> | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public interface Producer<T> | ||
{ | ||
T get(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...in/java/org/jboss/forge/furnace/container/simple/impl/SimpleProducerExportedInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.furnace.container.simple.impl; | ||
|
||
import org.jboss.forge.furnace.addons.Addon; | ||
import org.jboss.forge.furnace.container.simple.Producer; | ||
import org.jboss.forge.furnace.exception.ContainerException; | ||
import org.jboss.forge.furnace.proxy.ClassLoaderInterceptor; | ||
import org.jboss.forge.furnace.proxy.Proxies; | ||
import org.jboss.forge.furnace.spi.ExportedInstance; | ||
|
||
/** | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
* | ||
*/ | ||
public class SimpleProducerExportedInstance<T> implements ExportedInstance<T> | ||
{ | ||
private final Addon addon; | ||
private final Class<T> type; | ||
private final Class<? extends Producer<T>> producer; | ||
private final boolean singleton; | ||
private T delegate; | ||
|
||
public SimpleProducerExportedInstance(Addon addon, Class<T> type, Class<? extends Producer<T>> producer, | ||
boolean singleton) | ||
{ | ||
this.addon = addon; | ||
this.type = type; | ||
this.producer = producer; | ||
this.singleton = singleton; | ||
} | ||
|
||
@Override | ||
public T get() | ||
{ | ||
if (singleton) | ||
{ | ||
if (delegate == null) | ||
{ | ||
delegate = newInstance(); | ||
} | ||
return delegate; | ||
} | ||
else | ||
{ | ||
return newInstance(); | ||
} | ||
} | ||
|
||
private T newInstance() | ||
{ | ||
try | ||
{ | ||
T delegate = producer.newInstance().get(); | ||
delegate = Proxies.enhance(addon.getClassLoader(), delegate, | ||
new ClassLoaderInterceptor(addon.getClassLoader(), | ||
delegate)); | ||
return delegate; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new ContainerException("Could not create instance of [" + type.getName() + "] through reflection.", | ||
e); | ||
} | ||
} | ||
|
||
@Override | ||
public void release(T instance) | ||
{ | ||
// no action required | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return type.getName() + " from " + addon; | ||
} | ||
|
||
@Override | ||
public Class<? extends T> getActualType() | ||
{ | ||
return type; | ||
} | ||
|
||
@Override | ||
public Addon getSourceAddon() | ||
{ | ||
return addon; | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((addon == null) ? 0 : addon.hashCode()); | ||
result = prime * result + ((type == null) ? 0 : type.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) | ||
{ | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
SimpleProducerExportedInstance other = (SimpleProducerExportedInstance) obj; | ||
if (addon == null) | ||
{ | ||
if (other.addon != null) | ||
return false; | ||
} | ||
else if (!addon.equals(other.addon)) | ||
return false; | ||
if (type == null) | ||
{ | ||
if (other.type != null) | ||
return false; | ||
} | ||
else if (!type.equals(other.type)) | ||
return false; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...rc/test/java/org/jboss/forge/furnace/container/simple/impl/SimpleServiceRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.furnace.container.simple.impl; | ||
|
||
import org.jboss.forge.furnace.container.simple.Producer; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* | ||
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a> | ||
*/ | ||
public class SimpleServiceRegistryTest | ||
{ | ||
|
||
@Test | ||
public void testExtractProducesType() | ||
{ | ||
Class<?> type = SimpleServiceRegistry.extractProducesType(MyProducer.class); | ||
Assert.assertSame(String.class, type); | ||
} | ||
|
||
class MyProducer implements Producer<String> | ||
{ | ||
@Override | ||
public String get() | ||
{ | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.