Skip to content

Commit

Permalink
using modelProvider to get Class object
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashi Bhushan committed Aug 23, 2016
1 parent 572e333 commit 0c74dd9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
5 changes: 0 additions & 5 deletions slice-core-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@
<artifactId>org.osgi.core</artifactId>
</dependency>

<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.classloader</artifactId>
</dependency>

<!-- javax -->
<dependency>
<groupId>javax.servlet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.cognifide.slice.api.injector.InjectorWithContext;
import com.cognifide.slice.api.injector.InjectorsRepository;
import com.cognifide.slice.api.provider.ModelProvider;
import org.apache.sling.commons.classloader.DynamicClassLoaderManager;

public final class SliceTagUtils {

Expand Down Expand Up @@ -83,11 +82,24 @@ public static <T> T getFromCurrentPath(final PageContext pageContext, final Clas
* @throws ClassNotFoundException if the class was not found
*/
public static Class<?> getClassFromType(final PageContext pageContext, final String type) throws ClassNotFoundException {
final SlingScriptHelper scriptHelper = getSlingScriptHelper(pageContext);
final DynamicClassLoaderManager dynamicClassLoaderManager = scriptHelper
.getService(DynamicClassLoaderManager.class);
final ClassLoader classLoader = dynamicClassLoaderManager.getDynamicClassLoader();
return classLoader.loadClass(type);
final SlingHttpServletRequest request = SliceTagUtils.slingRequestFrom(pageContext);
final InjectorsRepository injectorsRepository = SliceTagUtils.injectorsRepositoryFrom(pageContext);

final String injectorName = getInjectorName(request, null, injectorsRepository);

final InjectorWithContext injector = injectorsRepository.getInjector(injectorName);
if (injector == null) {
throw new IllegalStateException("Guice injector not found: " + injectorName);
}
injector.pushContextProvider(contextProviderFrom(pageContext));

final ModelProvider modelProvider = injector.getInstance(ModelProvider.class);

try {
return modelProvider.get(type, request.getResource()).getClass();
} finally {
injector.popContextProvider();
}
}

/**
Expand Down

0 comments on commit 0c74dd9

Please sign in to comment.