Skip to content

Commit

Permalink
Improve ResourceSupplier API
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Dec 28, 2015
1 parent 5da32e3 commit 539a122
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 50 deletions.
19 changes: 16 additions & 3 deletions classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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.teavm.classlib;

import org.teavm.model.ListableClassReaderSource;

/**
*
* @author Alexey Andreev
*/
public interface ResourceSupplier {
String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource);
String[] supplyResources(ResourceSupplierContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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.teavm.classlib;

import java.util.Properties;
import org.teavm.model.ListableClassReaderSource;

/**
*
* @author Alexey Andreev
*/
public interface ResourceSupplierContext {
ClassLoader getClassLoader();

ListableClassReaderSource getClassSource();

Properties getProperties();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.HashSet;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.teavm.classlib.ResourceSupplier;
import org.teavm.classlib.ResourceSupplierContext;
import org.teavm.codegen.SourceWriter;
import org.teavm.javascript.Renderer;
import org.teavm.javascript.spi.Injector;
import org.teavm.javascript.spi.InjectorContext;
import org.teavm.model.ListableClassReaderSource;
import org.teavm.model.MethodReference;

/**
Expand All @@ -35,8 +38,9 @@ private void generateSupplyResources(InjectorContext context) throws IOException

ClassLoader classLoader = context.getClassLoader();
Set<String> resourceSet = new HashSet<>();
SupplierContextImpl supplierContext = new SupplierContextImpl(context);
for (ResourceSupplier supplier : ServiceLoader.load(ResourceSupplier.class, classLoader)) {
String[] resources = supplier.supplyResources(classLoader, context.getClassSource());
String[] resources = supplier.supplyResources(supplierContext);
if (resources != null) {
resourceSet.addAll(Arrays.asList(resources));
}
Expand Down Expand Up @@ -65,4 +69,27 @@ private void generateSupplyResources(InjectorContext context) throws IOException
}
writer.outdent().append('}');
}

static class SupplierContextImpl implements ResourceSupplierContext {
InjectorContext injectorContext;

public SupplierContextImpl(InjectorContext injectorContext) {
this.injectorContext = injectorContext;
}

@Override
public ClassLoader getClassLoader() {
return injectorContext.getClassLoader();
}

@Override
public ListableClassReaderSource getClassSource() {
return injectorContext.getClassSource();
}

@Override
public Properties getProperties() {
return injectorContext.getProperties();
}
}
}
43 changes: 0 additions & 43 deletions html4j/src/main/java/org/teavm/html4j/HTML4jResourceSupplier.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
package org.teavm.classlib.java.lang;

import org.teavm.classlib.ResourceSupplier;
import org.teavm.model.ListableClassReaderSource;
import org.teavm.classlib.ResourceSupplierContext;

/**
*
* @author Alexey Andreev
*/
public class TestResourcesSupplier implements ResourceSupplier {
@Override
public String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource) {
public String[] supplyResources(ResourceSupplierContext context) {
String[] result = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
for (int i = 0; i < result.length; ++i) {
result[i] = "resources-for-test/" + result[i];
Expand Down

0 comments on commit 539a122

Please sign in to comment.