forked from openhab/openhab-core
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ActionService and ThingActions classes in Xtext cache (openhab…
…#1714) Xtext uses a cache for looking up classes when rules are run. It also adds a null class value to this cache when a class is not found. Once a value has entered the cache it will not be updated. This causes the cache to return the wrong class (or the null value) when calling static methods on ActionService and ThingActions classes that were added/updated. With the changes in this PR Xtext will be configured to use a custom cache that updates the ActionService and ThingActions class references. The PR also has a fix for the AnnotatedThingActionModuleTypeProvider not properly sending ModuleType removed events when all ThingActions registrations have been removed. Fixes openhab#1265 Fixes openhab#1694 Signed-off-by: Wouter Born <github@maindrain.net> GitOrigin-RevId: 0d1a15e
- Loading branch information
Showing
5 changed files
with
224 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...ensmarthouse.core.model.rule/src/org/openhab/core/model/rule/scoping/RulesClassCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.model.rule.scoping; | ||
|
||
import java.util.HashMap; | ||
|
||
import org.eclipse.xtext.common.types.access.impl.Primitives; | ||
import org.openhab.core.model.script.engine.action.ActionService; | ||
import org.openhab.core.thing.binding.ThingActions; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Deactivate; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.component.annotations.ReferenceCardinality; | ||
import org.osgi.service.component.annotations.ReferencePolicy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The class cache used by the {@link RulesClassFinder} for resolving classes in DSL rules. | ||
* | ||
* It allows for removing and updating classes in the cache when add-ons are installed or updated. | ||
* | ||
* @author Wouter Born - Initial contribution | ||
*/ | ||
@Component | ||
public class RulesClassCache extends HashMap<String, Class<?>> { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private static RulesClassCache instance; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(RulesClassCache.class); | ||
|
||
@Activate | ||
public RulesClassCache() { | ||
super(500); | ||
for (Class<?> primitiveType : Primitives.ALL_PRIMITIVE_TYPES) { | ||
put(primitiveType.getName(), primitiveType); | ||
} | ||
|
||
if (instance != null) { | ||
throw new IllegalStateException("RulesClassCache should only be activated once!"); | ||
} | ||
instance = this; | ||
} | ||
|
||
@Deactivate | ||
public void deactivate() { | ||
clear(); | ||
instance = null; | ||
} | ||
|
||
public static RulesClassCache getInstance() { | ||
return instance; | ||
} | ||
|
||
private void updateCacheEntry(Object object) { | ||
String key = object.getClass().getName(); | ||
put(key, object.getClass()); | ||
logger.debug("Updated cache entry: {}", key); | ||
} | ||
|
||
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) | ||
public void addActionService(ActionService actionService) { | ||
updateCacheEntry(actionService); | ||
} | ||
|
||
public void removeActionService(ActionService actionService) { | ||
} | ||
|
||
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) | ||
public void addThingActions(ThingActions thingActions) { | ||
updateCacheEntry(thingActions); | ||
} | ||
|
||
public void removeThingActions(ThingActions thingActions) { | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...nsmarthouse.core.model.rule/src/org/openhab/core/model/rule/scoping/RulesClassFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.model.rule.scoping; | ||
|
||
import org.eclipse.xtext.common.types.access.impl.ClassFinder; | ||
import org.eclipse.xtext.common.types.access.impl.ClassNameUtil; | ||
|
||
/** | ||
* This is a customized version of the {@link ClassFinder}. | ||
* | ||
* It allows for removing and updating classes in the cache when add-ons are installed or updated. | ||
* | ||
* @author Wouter Born - Initial contribution | ||
*/ | ||
public class RulesClassFinder extends ClassFinder { | ||
|
||
private static class Null { | ||
} | ||
|
||
private static final Class<?> NULL_CLASS = Null.class; | ||
|
||
private final ClassLoader classLoader; | ||
private final ClassNameUtil classNameUtil = new ClassNameUtil(); | ||
|
||
protected RulesClassFinder(ClassLoader classLoader) { | ||
super(classLoader); | ||
this.classLoader = classLoader; | ||
} | ||
|
||
@Override | ||
public Class<?> forName(String name) throws ClassNotFoundException { | ||
RulesClassCache cache = RulesClassCache.getInstance(); | ||
Class<?> result = cache.get(name); | ||
if (result != null) { | ||
if (result == NULL_CLASS) { | ||
throw CACHED_EXCEPTION; | ||
} | ||
return result; | ||
} | ||
|
||
try { | ||
result = forName(classNameUtil.normalizeClassName(name), classLoader); | ||
cache.put(name, result); | ||
return result; | ||
} catch (ClassNotFoundException e) { | ||
cache.put(name, NULL_CLASS); | ||
throw e; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
protected Class<?> forName(String name, ClassLoader classLoader) throws ClassNotFoundException { | ||
return Class.forName(name, false, classLoader); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...house.core.model.rule/src/org/openhab/core/model/rule/scoping/RulesJavaReflectAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.model.rule.scoping; | ||
|
||
import org.eclipse.xtext.common.types.access.impl.ClassFinder; | ||
import org.eclipse.xtext.common.types.util.JavaReflectAccess; | ||
|
||
import com.google.inject.Inject; | ||
|
||
/** | ||
* This is a customized version of {@link JavaReflectAccess}. | ||
* | ||
* It allows for removing and updating classes in the cache used by the {@link RulesClassFinder} when add-ons are | ||
* installed or updated. | ||
* | ||
* @author Wouter Born - Initial contribution | ||
*/ | ||
public class RulesJavaReflectAccess extends JavaReflectAccess { | ||
|
||
private ClassLoader classLoader = getClass().getClassLoader(); | ||
|
||
private ClassFinder classFinder; | ||
|
||
@Override | ||
@Inject(optional = true) | ||
public void setClassLoader(ClassLoader classLoader) { | ||
if (classLoader != this.classLoader) { | ||
this.classLoader = classLoader; | ||
classFinder = null; | ||
} | ||
} | ||
|
||
@Override | ||
public ClassFinder getClassFinder() { | ||
if (classFinder == null) { | ||
classFinder = new RulesClassFinder(classLoader); | ||
} | ||
return classFinder; | ||
} | ||
} |