use of org.openremote.model.asset.AssetDescriptor in project openremote by openremote.
the class PersistenceService method init.
@Override
public void init(Container container) throws Exception {
this.messageBrokerService = container.hasService(MessageBrokerService.class) ? container.getService(MessageBrokerService.class) : null;
String dbVendor = getString(container.getConfig(), DB_VENDOR, DB_VENDOR_DEFAULT).toUpperCase(Locale.ROOT);
LOG.info("Preparing persistence service for database: " + dbVendor);
try {
database = Database.Product.valueOf(dbVendor);
} catch (Exception e) {
LOG.severe("Requested DB_VENDOR is not supported: " + dbVendor);
throw new UnsupportedOperationException("Requested DB_VENDOR is not supported: " + dbVendor);
}
String dbHost = getString(container.getConfig(), DB_HOST, DB_HOST_DEFAULT);
int dbPort = getInteger(container.getConfig(), DB_PORT, DB_PORT_DEFAULT);
String dbName = getString(container.getConfig(), DB_NAME, DB_NAME_DEFAULT);
String dbSchema = getString(container.getConfig(), DB_SCHEMA, DB_SCHEMA_DEFAULT);
String dbUsername = getString(container.getConfig(), DB_USERNAME, DB_USERNAME_DEFAULT);
String dbPassword = getString(container.getConfig(), DB_PASSWORD, DB_PASSWORD_DEFAULT);
String connectionUrl = "jdbc:" + database.getConnectorName() + "://" + dbHost + ":" + dbPort + "/" + dbName;
connectionUrl = UriBuilder.fromUri(connectionUrl).replaceQueryParam("currentSchema", dbSchema).build().toString();
persistenceUnitProperties = database.createProperties();
if (messageBrokerService != null) {
persistenceUnitProperties.put(AvailableSettings.SESSION_SCOPED_INTERCEPTOR, PersistenceEventInterceptor.class.getName());
}
persistenceUnitProperties.put(AvailableSettings.DEFAULT_SCHEMA, dbSchema);
// Add custom integrator so we can register a custom flush entity event listener
persistenceUnitProperties.put(EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, IntegratorProvider.class.getName());
persistenceUnitName = getString(container.getConfig(), PERSISTENCE_UNIT_NAME, PERSISTENCE_UNIT_NAME_DEFAULT);
forceClean = getBoolean(container.getConfig(), SETUP_WIPE_CLEAN_INSTALL, container.isDevMode());
openDatabase(container, database, dbUsername, dbPassword, connectionUrl);
prepareSchema(connectionUrl, dbUsername, dbPassword, dbSchema);
// Register standard entity classes and also any Entity ClassProviders
List<String> entityClasses = new ArrayList<>(50);
entityClasses.add(Asset.class.getName());
entityClasses.add(UserAssetLink.class.getName());
entityClasses.add(AssetDatapoint.class.getName());
entityClasses.add(SentNotification.class.getName());
entityClasses.add(AssetPredictedDatapoint.class.getName());
entityClasses.add(Tenant.class.getName());
entityClasses.add(User.class.getName());
entityClasses.add(UserAttribute.class.getName());
entityClasses.add(RealmRole.class.getName());
entityClasses.add(GlobalRuleset.class.getName());
entityClasses.add(AssetRuleset.class.getName());
entityClasses.add(TenantRuleset.class.getName());
entityClasses.add(SyslogEvent.class.getName());
entityClasses.add(GatewayConnection.class.getName());
entityClasses.add(ConsoleAppConfig.class.getName());
entityClasses.add(ProvisioningConfig.class.getName());
entityClasses.add(X509ProvisioningConfig.class.getName());
// Add packages with package-info (don't think this is JPA spec but hibernate specific)
entityClasses.add("org.openremote.container.persistence");
entityClasses.add("org.openremote.container.util");
// Get asset sub type entities from asset model
Arrays.stream(ValueUtil.getAssetDescriptors(null)).map(AssetDescriptor::getType).filter(assetClass -> assetClass.getAnnotation(Entity.class) != null).map(Class::getName).forEach(entityClasses::add);
// Get any entity class providers from the service loader
ServiceLoader<EntityClassProvider> entityClassProviders = ServiceLoader.load(EntityClassProvider.class);
entityClassProviders.forEach(entityClassProvider -> entityClassProvider.getEntityClasses().stream().filter(entityClass -> entityClass.getAnnotation(Entity.class) != null).map(Class::getName).forEach(entityClasses::add));
this.entityManagerFactory = getEntityManagerFactory(persistenceUnitProperties, entityClasses);
// Persistence.createEntityManagerFactory(persistenceUnitName, persistenceUnitProperties);
}
use of org.openremote.model.asset.AssetDescriptor in project openremote by openremote.
the class ValueUtil method doInitialise.
/**
* Initialise the asset model and throw an {@link IllegalStateException} exception if a problem is detected; this
* can be called by applications at startup to fail hard and fast if the asset model is un-usable
*/
protected static void doInitialise() throws IllegalStateException {
assetInfoMap = new HashMap<>();
assetTypeMap = new HashMap<>();
agentLinkMap = new HashMap<>();
metaItemDescriptors = new ArrayList<>();
valueDescriptors = new ArrayList<>();
generator = null;
// Provide basic Object Mapper and enhance once asset model is initialised
JSON = configureObjectMapper(new ObjectMapper());
LOG.info("Initialising asset model...");
Map<Class<? extends Asset<?>>, List<NameHolder>> assetDescriptorProviders = new TreeMap<>(new ClassHierarchyComparator());
// noinspection RedundantCast
assetDescriptorProviders.put((Class<? extends Asset<?>>) (Class<?>) Asset.class, new ArrayList<>(getDescriptorFields(Asset.class)));
getModelProviders().forEach(assetModelProvider -> {
LOG.fine("Processing asset model provider: " + assetModelProvider.getClass().getSimpleName());
LOG.fine("Auto scan = " + assetModelProvider.useAutoScan());
if (assetModelProvider.useAutoScan()) {
Set<Class<? extends Asset<?>>> assetClasses = getAssetClasses(assetModelProvider);
LOG.fine("Found " + assetClasses.size() + " asset class(es)");
assetClasses.forEach(assetClass -> assetDescriptorProviders.computeIfAbsent(assetClass, aClass -> new ArrayList<>(getDescriptorFields(aClass))));
ModelDescriptors modelDescriptors = assetModelProvider.getClass().getAnnotation(ModelDescriptors.class);
if (modelDescriptors != null) {
for (ModelDescriptor modelDescriptor : modelDescriptors.value()) {
Class<? extends Asset<?>> assetClass = (Class<? extends Asset<?>>) modelDescriptor.assetType();
assetDescriptorProviders.compute(assetClass, (aClass, list) -> {
if (list == null) {
list = new ArrayList<>();
}
list.addAll(getDescriptorFields(modelDescriptor.provider()));
return list;
});
}
}
}
if (assetModelProvider.getAssetDescriptors() != null) {
for (AssetDescriptor<?> assetDescriptor : assetModelProvider.getAssetDescriptors()) {
Class<? extends Asset<?>> assetClass = assetDescriptor.getType();
assetDescriptorProviders.compute(assetClass, (aClass, list) -> {
if (list == null) {
list = new ArrayList<>();
}
list.add(assetDescriptor);
return list;
});
}
}
if (assetModelProvider.getAttributeDescriptors() != null) {
assetModelProvider.getAttributeDescriptors().forEach((assetClass, attributeDescriptors) -> assetDescriptorProviders.compute(assetClass, (aClass, list) -> {
if (list == null) {
list = new ArrayList<>();
}
list.addAll(attributeDescriptors);
return list;
}));
}
if (assetModelProvider.getMetaItemDescriptors() != null) {
assetModelProvider.getMetaItemDescriptors().forEach((assetClass, metaDescriptors) -> assetDescriptorProviders.compute(assetClass, (aClass, list) -> {
if (list == null) {
list = new ArrayList<>();
}
list.addAll(metaDescriptors);
return list;
}));
}
if (assetModelProvider.getValueDescriptors() != null) {
assetModelProvider.getValueDescriptors().forEach((assetClass, valueDescriptors) -> assetDescriptorProviders.compute(assetClass, (aClass, list) -> {
if (list == null) {
list = new ArrayList<>();
}
list.addAll(valueDescriptors);
return list;
}));
}
});
// Build each asset info checking that no conflicts occur
Map<Class<? extends Asset<?>>, List<NameHolder>> copy = new HashMap<>(assetDescriptorProviders);
assetDescriptorProviders.forEach((assetClass, descriptors) -> {
// Skip abstract classes as a start point - they should be in the class hierarchy of concrete class
if (!Modifier.isAbstract(assetClass.getModifiers())) {
AssetTypeInfo assetInfo = buildAssetInfo(assetClass, copy);
assetInfoMap.put(assetClass, assetInfo);
assetTypeMap.put(assetInfo.getAssetDescriptor().getName(), assetClass);
if (assetInfo.getAssetDescriptor() instanceof AgentDescriptor) {
AgentDescriptor<?, ?, ?> agentDescriptor = (AgentDescriptor<?, ?, ?>) assetInfo.getAssetDescriptor();
String agentLinkName = agentDescriptor.getAgentLinkClass().getSimpleName();
if (agentLinkMap.containsKey(agentLinkName) && agentLinkMap.get(agentLinkName) != agentDescriptor.getAgentLinkClass()) {
throw new IllegalStateException("AgentLink simple class name must be unique, duplicate found for: " + agentDescriptor.getAgentLinkClass());
}
agentLinkMap.put(agentLinkName, agentDescriptor.getAgentLinkClass());
}
}
});
// Check each value type implements serializable interface
List<ValueDescriptor<?>> nonSerializableValueDescriptors = new ArrayList<>();
valueDescriptors.forEach(vd -> {
if (!Serializable.class.isAssignableFrom(vd.getType())) {
nonSerializableValueDescriptors.add(vd);
}
});
if (!nonSerializableValueDescriptors.isEmpty()) {
String vds = nonSerializableValueDescriptors.stream().map(ValueDescriptor::toString).collect(Collectors.joining(",\n"));
throw new IllegalStateException("One or more value types do not implement java.io.Serializable: " + vds);
}
// Call on finished on each provider
assetModelProviders.forEach(AssetModelProvider::onAssetModelFinished);
// Add agent link sub types to object mapper (need to avoid circular dependency)
NamedType[] agentLinkSubTypes = Arrays.stream(getAgentLinkClasses()).map(agentLinkClass -> new NamedType(agentLinkClass, agentLinkClass.getSimpleName())).toArray(NamedType[]::new);
JSON.registerSubtypes(agentLinkSubTypes);
doSchemaInit();
}
Aggregations