Java tutorial
/** * 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.module.clinicalsummary.rule; import java.io.IOException; import java.net.URL; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.openmrs.logic.rule.provider.ClassRuleProvider; import org.openmrs.module.ModuleClassLoader; import org.openmrs.module.ModuleFactory; import org.springframework.asm.ClassReader; import org.springframework.stereotype.Component; /** */ @Component public class EvaluableSummaryRuleProvider extends ClassRuleProvider { private static final Log log = LogFactory.getLog(EvaluableSummaryRuleProvider.class); private static final String CLINICAL_SUMMARY_MODULE = "clinicalsummary"; private static final String CLINICAL_SUMMARY_MODULE_API = "clinicalsummary-api"; private static final String RULE_CLASS_SUFFIX = "Rule"; private static final String CLASS_SUFFIX = "class"; /** * @see org.openmrs.logic.rule.provider.RuleProvider#afterStartup() */ @Override public void afterStartup() { if (log.isDebugEnabled()) log.debug("Registering clinical summary rules ..."); try { ModuleClassLoader moduleClassLoader = ModuleFactory.getModuleClassLoader(CLINICAL_SUMMARY_MODULE); for (URL url : moduleClassLoader.getURLs()) { String filename = url.getFile(); if (StringUtils.contains(filename, CLINICAL_SUMMARY_MODULE_API)) { ZipFile zipFile = new ZipFile(filename); for (Enumeration list = zipFile.entries(); list.hasMoreElements();) { ZipEntry entry = (ZipEntry) list.nextElement(); String zipFileName = FilenameUtils.getBaseName(entry.getName()); String zipFileExtension = FilenameUtils.getExtension(entry.getName()); if (StringUtils.endsWith(zipFileName, RULE_CLASS_SUFFIX) && StringUtils.equals(zipFileExtension, CLASS_SUFFIX)) { EvaluableRuleVisitor visitor = new EvaluableRuleVisitor(); ClassReader classReader = new ClassReader(zipFile.getInputStream(entry)); classReader.accept(visitor, false); } } zipFile.close(); } } } catch (IOException e) { log.error("Processing rule classes failed. Rule might not get registered."); } } }