Skip to content

Method: LazyLoadingProxyFactory(UnitOfWork)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.proxy.lazy;
19:
20: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
21: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
22: import cz.cvut.kbss.jopa.model.metamodel.ListAttribute;
23: import cz.cvut.kbss.jopa.proxy.lazy.gen.LazyLoadingEntityProxy;
24: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
25:
26: import java.lang.reflect.InvocationTargetException;
27: import java.util.List;
28: import java.util.Map;
29: import java.util.Set;
30:
31: /**
32: * Creates lazy-loading proxies for entity attributes.
33: * <p>
34: * Needs a {@link UnitOfWork} to associate the proxies with, so that lazy loading can be executed when triggered.
35: */
36: public class LazyLoadingProxyFactory {
37:
38: private final UnitOfWork uow;
39:
40: public LazyLoadingProxyFactory(UnitOfWork uow) {
41: this.uow = uow;
42: }
43:
44: /**
45: * Creates a lazy loading proxy for the value of the specified attribute.
46: *
47: * @param entity Entity whose attribute will be proxied
48: * @param fieldSpec Attribute to proxy
49: * @param <T> Entity type
50: * @return Lazy loading proxy associated with a persistence context
51: */
52: public <T> Object createProxy(T entity, FieldSpecification<? super T, ?> fieldSpec) {
53: final Class<?> type = fieldSpec.getJavaType();
54: if (List.class.isAssignableFrom(type)) {
55: return new LazyLoadingListProxy<>(entity, (ListAttribute<T, ?>) fieldSpec, uow);
56: } else if (Set.class.isAssignableFrom(type)) {
57: return new LazyLoadingSetProxy<>(entity, (FieldSpecification) fieldSpec, uow);
58: } else if (Map.class.isAssignableFrom(type)) {
59: return new LazyLoadingMapProxy<>(entity, (FieldSpecification) fieldSpec, uow);
60: } else if (uow.getMetamodel().isEntityType(type)) {
61: try {
62: final Class<?> proxyType = uow.getMetamodel().getLazyLoadingProxy(type);
63: final LazyLoadingEntityProxy<?> proxy = (LazyLoadingEntityProxy<?>) proxyType.getDeclaredConstructor().newInstance();
64: proxy.setOwner(entity);
65: proxy.setPersistenceContext(uow);
66: proxy.setFieldSpec(fieldSpec);
67: return proxy;
68: } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
69: throw new OWLPersistenceException("Unable to instantiate lazy loading proxy!", e);
70: }
71: }
72: throw new IllegalArgumentException("Unsupported type for lazy proxying.");
73: }
74: }