Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRUNK-3644 Define which resources to load for module based on OpenMRS…
… version (cherry picked from commit a47d42e)
- Loading branch information
1 parent
5899a69
commit ee0d7ca
Showing
19 changed files
with
1,060 additions
and
153 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
37 changes: 37 additions & 0 deletions
37
api/src/main/java/org/openmrs/annotation/OpenmrsProfile.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,37 @@ | ||
/** | ||
* The contents of this file are subject to the OpenMRS Public License | ||
* Version 1.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://license.openmrs.org | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" | ||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing rights and limitations | ||
* under the License. | ||
* | ||
* Copyright (C) OpenMRS, LLC. All Rights Reserved. | ||
*/ | ||
package org.openmrs.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Placed on beans which should be loaded conditionally based on OpenMRS version or started modules. | ||
* | ||
* @since 1.10, 1.9.8, 1.8.5, 1.7.5 | ||
*/ | ||
@Target( { ElementType.TYPE }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
@Documented | ||
public @interface OpenmrsProfile { | ||
|
||
public String openmrsVersion() default ""; | ||
|
||
public String[] modules() default {}; | ||
} |
82 changes: 82 additions & 0 deletions
82
api/src/main/java/org/openmrs/annotation/OpenmrsProfileExclusionFilter.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,82 @@ | ||
/** | ||
* The contents of this file are subject to the OpenMRS Public License | ||
* Version 1.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://license.openmrs.org | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" | ||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing rights and limitations | ||
* under the License. | ||
* | ||
* Copyright (C) OpenMRS, LLC. All Rights Reserved. | ||
*/ | ||
package org.openmrs.annotation; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.openmrs.module.Module; | ||
import org.openmrs.module.ModuleFactory; | ||
import org.openmrs.module.ModuleUtil; | ||
import org.openmrs.util.OpenmrsConstants; | ||
import org.springframework.core.type.classreading.MetadataReader; | ||
import org.springframework.core.type.classreading.MetadataReaderFactory; | ||
import org.springframework.core.type.filter.TypeFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* Creates a bean only if profile is matched. It returns true if a bean should be excluded. | ||
*/ | ||
public class OpenmrsProfileExclusionFilter implements TypeFilter { | ||
|
||
/** | ||
* @param metadataReader | ||
* @param metadataReaderFactory | ||
* @return | ||
* @throws IOException | ||
* | ||
* @should not include bean for openmrs from 1_6 to 1_8 | ||
* @should not include bean for openmrs 1_10 and later | ||
* @should not include bean for openmrs 1_9 and later if module missing | ||
* @should include bean for openmrs 1_9 and later | ||
*/ | ||
@Override | ||
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { | ||
Map<String, Object> openmrsProfile = metadataReader.getAnnotationMetadata().getAnnotationAttributes( | ||
"org.openmrs.annotation.OpenmrsProfile"); | ||
if (openmrsProfile != null) { | ||
Object openmrsVersion = openmrsProfile.get("openmrsVersion"); | ||
if (StringUtils.isNotBlank((String) openmrsVersion)) { | ||
if (!ModuleUtil.matchRequiredVersions(OpenmrsConstants.OPENMRS_VERSION_SHORT, (String) openmrsVersion)) { | ||
return true; //exclude | ||
} | ||
} | ||
|
||
String[] modules = (String[]) openmrsProfile.get("modules"); | ||
|
||
for (String moduleAndVersion : modules) { | ||
boolean exclude = true; | ||
|
||
String[] splitModuleAndVersion = moduleAndVersion.split(":"); | ||
String moduleId = splitModuleAndVersion[0]; | ||
String moduleVersion = splitModuleAndVersion[1]; | ||
|
||
for (Module module : ModuleFactory.getStartedModules()) { | ||
if (module.getModuleId().equals(moduleId)) { | ||
if (ModuleUtil.matchRequiredVersions(module.getVersion(), moduleVersion)) { | ||
exclude = false; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (exclude) { | ||
return exclude; | ||
} | ||
} | ||
} | ||
|
||
return false; //include | ||
} | ||
} |
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
Oops, something went wrong.