Skip to content

Commit 6381861

Browse files
committed
E
1 parent 662d71a commit 6381861

9 files changed

Lines changed: 128 additions & 39 deletions

File tree

modules/packed-incubator/packed-incubator-concurrent/src/main/java/app/packed/concurrent/JobOverviewMirror.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
import java.util.stream.Stream;
1919

20-
import app.packed.component.ComponentRealm;
2120
import app.packed.extension.BaseExtension;
2221
import app.packed.namespace.OverviewHandle;
2322
import app.packed.namespace.OverviewMirror;
23+
import app.packed.operation.OperationMirror;
2424

2525
/**
2626
* A mirror for a job namespace. All jobs within a
@@ -43,21 +43,16 @@ public Stream<DaemonJobMirror> allDaemons() {
4343

4444
/** {@return a stream of all jobs that have been defined in the namespace or descendant namespaces} */
4545
public Stream<JobMirror> allJobs() {
46-
return Stream.of();
46+
return operations(JobMirror.class).map(j -> j);
4747
}
4848

4949
/** {@return a stream of all jobs that have been defined in the namespace} */
50-
public Stream<DaemonJobMirror> daemons() {
51-
return jobs().filter(DaemonJobMirror.class::isInstance).map(DaemonJobMirror.class::cast);
50+
public OperationMirror.OfStream<DaemonJobMirror> daemons() {
51+
return operations(DaemonJobMirror.class);
5252
}
5353

5454
/** {@return a stream of all jobs that have been defined in the namespace} */
55-
public Stream<JobMirror> jobs() {
56-
return Stream.of();
57-
}
58-
59-
/** {@return the owner of the namespace} */
60-
public ComponentRealm owner() {
61-
return ComponentRealm.userland();
55+
public OperationMirror.OfStream<JobMirror> jobs() {
56+
return operations(JobMirror.class);
6257
}
6358
}

modules/packed-incubator/packed-incubator-concurrent/src/main/java/app/packed/concurrent/job/ScTest2.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import app.packed.assembly.BaseAssembly;
2525
import app.packed.concurrent.DaemonJob;
2626
import app.packed.concurrent.DaemonJobContext;
27+
import app.packed.concurrent.JobOverviewMirror;
2728
import app.packed.lifecycle.Stop;
2829
import app.packed.web.HttpContext;
2930
import app.packed.web.WebGet;
@@ -34,13 +35,18 @@
3435
public class ScTest2 extends BaseAssembly {
3536

3637
public static void main(String[] args) throws Exception {
37-
// ApplicationMirror m = App.mirrorOf(new ScTest2());
38+
ApplicationMirror m = App.mirrorOf(new ScTest2());
39+
40+
System.out.println(m.overview(JobOverviewMirror.class).allDaemons().toList());
41+
3842
// Optional<ThreadNamespaceMirror> o = m.namespace(ThreadNamespaceMirror.class);
3943

4044
// o.get().daemons().forEach(c -> {
4145
// IO.println(c.name() + ":" + c.isInteruptAtStop());
4246
// });
4347

48+
49+
4450
App app = App.start(new ScTest2());
4551
Thread.sleep(30000);
4652
app.stop();
@@ -52,8 +58,8 @@ public static void main(String[] args) throws Exception {
5258
@Override
5359
protected void build() {
5460

55-
link(assembly().finder().findOne("foo.bar"));
56-
assembly().finder().serviceLoader(BaseAssembly.class).forEach(e -> link(e));
61+
// link(assembly().finder().findOne("foo.bar"));
62+
// assembly().finder().serviceLoader(BaseAssembly.class).forEach(e -> link(e));
5763

5864
provideInstance("asdasd");
5965
install(MuB.class);

modules/packed/src/main/java/app/packed/application/ApplicationMirror.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import internal.app.packed.application.ApplicationSetup;
2626
import internal.app.packed.bean.BeanSetup;
2727
import internal.app.packed.bean.introspection.IntrospectorOnAutoService;
28+
import internal.app.packed.component.PackedOverviewHandle;
2829
import internal.app.packed.container.ContainerSetup;
2930
import internal.app.packed.extension.ExtensionSetup;
3031
import internal.app.packed.extension.base.BaseExtensionBeanIntrospector;
@@ -212,7 +213,7 @@ public OperationMirror.OfStream<OperationMirror> operations() {
212213
}
213214

214215
public <O extends OverviewMirror<?>> O overview(Class<O> type) {
215-
throw new UnsupportedOperationException();
216+
return PackedOverviewHandle.ofApplication(application, type);
216217
}
217218

218219
public void print() {

modules/packed/src/main/java/app/packed/namespace/OverviewMirror.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ protected OverviewMirror(OverviewHandle<E> overviewHandle) {
4343
}
4444

4545
protected final OperationMirror.OfStream<OperationMirror> operations() {
46-
throw new UnsupportedOperationException();
46+
return handle.operations();
4747
}
4848

4949
protected final <T extends OperationMirror> OperationMirror.OfStream<T> operations(Class<T> operations) {
50-
throw new UnsupportedOperationException();
50+
return handle.operations(operations);
5151
}
5252
}

modules/packed/src/main/java/internal/app/packed/bean/BeanSetup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.function.Function;
1212
import java.util.function.Supplier;
1313

14+
import org.jspecify.annotations.Nullable;
15+
1416
import app.packed.bean.BeanConfiguration;
1517
import app.packed.bean.BeanHandle;
1618
import app.packed.bean.BeanInstaller;
@@ -27,8 +29,6 @@
2729
import app.packed.extension.Extension;
2830
import app.packed.lifecycle.LifecycleKind;
2931
import app.packed.operation.Op;
30-
import org.jspecify.annotations.Nullable;
31-
3232
import internal.app.packed.bean.introspection.BeanScanner;
3333
import internal.app.packed.bean.sidehandle.PackedSidehandle;
3434
import internal.app.packed.binding.BindingProvider;

modules/packed/src/main/java/internal/app/packed/component/PackedOverviewHandle.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,74 @@
1717

1818
import static java.util.Objects.requireNonNull;
1919

20+
import app.packed.application.ApplicationMirror;
2021
import app.packed.extension.Extension;
21-
import app.packed.extension.ExtensionNamespace;
2222
import app.packed.namespace.OverviewHandle;
23+
import app.packed.namespace.OverviewMirror;
24+
import app.packed.operation.OperationMirror;
25+
import internal.app.packed.application.ApplicationSetup;
26+
import internal.app.packed.extension.ExtensionClassModel;
27+
import internal.app.packed.invoke.ConstructorSupport;
28+
import internal.app.packed.invoke.ConstructorSupport.OverviewMirrorFactory;
29+
import internal.app.packed.util.types.TypeVariableExtractor;
2330

2431
/**
2532
*
2633
*/
2734
public abstract sealed class PackedOverviewHandle<E extends Extension<E>> implements OverviewHandle<E> {
2835

29-
final ExtensionNamespace<?, ?> owner;
36+
/** Extract the extension type variable from OverviewMirror. */
37+
private static final ClassValue<Class<? extends Extension<?>>> EXTENSION_TYPES = new ClassValue<>() {
3038

31-
public PackedOverviewHandle(ExtensionNamespace<?, ?> owner) {
32-
this.owner = requireNonNull(owner);
39+
private static final TypeVariableExtractor EXTRACTOR = TypeVariableExtractor.of(OverviewMirror.class);
40+
41+
@Override
42+
protected Class<? extends Extension<?>> computeValue(Class<?> type) {
43+
return ExtensionClassModel.extractE(EXTRACTOR, type);
44+
}
45+
};
46+
47+
/** Cache OverviewMirror constructor factories. */
48+
private static final ClassValue<OverviewMirrorFactory> CONSTRUCTORS = new ClassValue<>() {
49+
50+
@SuppressWarnings("unchecked")
51+
@Override
52+
protected OverviewMirrorFactory computeValue(Class<?> type) {
53+
return ConstructorSupport.findOverviewMirrorConstructor((Class<? extends OverviewMirror<?>>) type);
54+
}
55+
};
56+
57+
@SuppressWarnings("unchecked")
58+
public static <O extends OverviewMirror<?>> O ofApplication(ApplicationSetup application, Class<O> type) {
59+
requireNonNull(type, "type is null");
60+
61+
Class<? extends Extension<?>> extensionType = EXTENSION_TYPES.get(type);
62+
OverviewMirrorFactory factory = CONSTRUCTORS.get(type);
63+
64+
PackedApplicationOverviewHandle<?> handle = new PackedApplicationOverviewHandle<>(application.mirror(), extensionType);
65+
66+
return (O) factory.create(handle);
3367
}
3468

3569
public static final class PackedApplicationOverviewHandle<E extends Extension<E>> extends PackedOverviewHandle<E> {
3670

37-
public PackedApplicationOverviewHandle(ExtensionNamespace<?, ?> owner) {
38-
super(owner);
71+
private final ApplicationMirror applicationMirror;
72+
73+
private final Class<? extends Extension<?>> extensionType;
74+
75+
public PackedApplicationOverviewHandle(ApplicationMirror applicationMirror, Class<? extends Extension<?>> extensionType) {
76+
this.applicationMirror = requireNonNull(applicationMirror);
77+
this.extensionType = requireNonNull(extensionType);
3978
}
4079

80+
@Override
81+
public OperationMirror.OfStream<OperationMirror> operations() {
82+
return applicationMirror.operations().filter(op -> op.installedByExtension() == extensionType);
83+
}
84+
85+
@Override
86+
public <T extends OperationMirror> OperationMirror.OfStream<T> operations(Class<T> operationType) {
87+
return (OperationMirror.OfStream<T>) operations().ofType(operationType);
88+
}
4189
}
4290
}

modules/packed/src/main/java/internal/app/packed/extension/ExtensionNamespaceSetup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public final class ExtensionNamespaceSetup extends NamespaceSetup {
7474

7575
public BaseExtensionNamespace base() {
7676
return (BaseExtensionNamespace) extensionNamespaceInstances.computeIfAbsent(BaseExtension.class, _ -> {
77-
ExtensionNamespaceFactory nf = model.namespaceFactory;
77+
ExtensionNamespaceFactory nf = ExtensionClassModel.of(BaseExtension.class).namespaceFactory;
7878
return nf.create(new PackedExtensionNamespaceHandle<>(this));
7979
});
8080
}

modules/packed/src/main/java/internal/app/packed/extension/ExtensionSetup.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,7 @@ public ComponentRealm owner() {
181181
public MainServiceNamespaceHandle services() {
182182
MainServiceNamespaceHandle s = sm;
183183
if (s == null) {
184-
s = namespace.base().services;
185-
// ExtensionSetup base = container.baseExtension();
186-
// base.n
187-
//
188-
// namespace.servicesToResolve
189-
//
190-
//// MainServiceNamespaceHandle par = treeParent == null ? null : treeParent.services();
191-
//// ExtensionHandle<BaseExtension> eh = new PackedExtensionHandle<>(container.baseExtension());
192-
////
193-
//// s = this.sm = eh.namespaceLazy(MainServiceNamespaceHandle.TEMPLATE, owner());
194-
////
195-
//
196-
// s.init(null, container);
184+
s = sm = container.servicesMain();
197185
}
198186
return s;
199187
}

modules/packed/src/main/java/internal/app/packed/invoke/ConstructorSupport.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import app.packed.extension.ExtensionNamespace;
3434
import app.packed.extension.ExtensionNamespaceHandle;
3535
import app.packed.extension.InternalExtensionException;
36+
import app.packed.namespace.OverviewHandle;
37+
import app.packed.namespace.OverviewMirror;
3638
import internal.app.packed.assembly.AssemblyClassModel;
3739
import internal.app.packed.build.hooks.BuildHook;
3840
import internal.app.packed.build.hooks.UseBuildHooks;
@@ -226,6 +228,38 @@ public static ExtensionNamespaceFactory findExtensionNamespaceConstructor(Class<
226228
return new ExtensionNamespaceFactory(mh);
227229
}
228230

231+
public static OverviewMirrorFactory findOverviewMirrorConstructor(Class<? extends OverviewMirror<?>> overviewMirrorClass) {
232+
if (Modifier.isAbstract(overviewMirrorClass.getModifiers())) {
233+
throw new InternalExtensionException("OverviewMirror " + StringFormatter.format(overviewMirrorClass) + " cannot be an abstract class");
234+
} else if (ClassUtil.isInnerOrLocal(overviewMirrorClass)) {
235+
throw new InternalExtensionException("OverviewMirror " + StringFormatter.format(overviewMirrorClass) + " cannot be an inner or local class");
236+
}
237+
238+
// An OverviewMirror must provide exactly one constructor
239+
Constructor<?>[] constructors = overviewMirrorClass.getDeclaredConstructors();
240+
if (constructors.length != 1) {
241+
throw new InternalExtensionException(StringFormatter.format(overviewMirrorClass) + " must declare exactly 1 constructor");
242+
}
243+
244+
Constructor<?> constructor = constructors[0];
245+
if (constructor.getParameterCount() != 1 || constructor.getParameterTypes()[0] != OverviewHandle.class) {
246+
throw new InternalExtensionException(overviewMirrorClass
247+
+ " must provide a constructor taking OverviewHandle, but constructor required " + Arrays.toString(constructor.getParameters()));
248+
}
249+
250+
// Create a MethodHandle for the constructor
251+
MethodHandle mh;
252+
try {
253+
Lookup l = MethodHandles.privateLookupIn(overviewMirrorClass, MethodHandles.lookup());
254+
mh = l.unreflectConstructor(constructor);
255+
} catch (IllegalAccessException e) {
256+
throw new InternalExtensionException(illegalAccessExtensionMsg(overviewMirrorClass), e);
257+
}
258+
// cast from (ConcreteOverviewMirror) -> (OverviewMirror)
259+
mh = MethodHandles.explicitCastArguments(mh, MethodType.methodType(OverviewMirror.class, OverviewHandle.class));
260+
return new OverviewMirrorFactory(mh);
261+
}
262+
229263
public static void forceLoad(Class<? extends Extension<?>> extensionClass) {
230264
// Ensure that the class initializer of the extension has been run before we progress
231265
try {
@@ -296,6 +330,23 @@ public Extension<?> create(ExtensionSetup extension) {
296330
}
297331
}
298332

333+
/** A factory class for creating instances of {@link OverviewMirror} */
334+
public static final class OverviewMirrorFactory {
335+
private final MethodHandle mh; // (OverviewHandle)OverviewMirror
336+
337+
public OverviewMirrorFactory(MethodHandle mh) {
338+
this.mh = requireNonNull(mh);
339+
}
340+
341+
public OverviewMirror<?> create(OverviewHandle<?> handle) {
342+
try {
343+
return (OverviewMirror<?>) mh.invokeExact(handle);
344+
} catch (Throwable e) {
345+
throw new InternalExtensionException("An instance of the overview mirror could not be created.", e);
346+
}
347+
}
348+
}
349+
299350
/** A factory class for creating instances of {@link Extension} */
300351
public static final class ExtensionNamespaceFactory {
301352
private final MethodHandle mh; // (ExtensionHandle)Extension

0 commit comments

Comments
 (0)