public class TestRuleBase implements RuleBase { List<Rule> rules = CollectUtils.newArrayList(); public List<Rule> getRules() { return rules; } }
static { @SuppressWarnings({"unchecked", "rawtypes"}) Map<String, String> origin = new HashMap(System.getProperties()); Map<String, String> properties = CollectUtils.newHashMap(origin); os = new Os(properties); user = new User(properties); java = new Java(properties); jvm = new Jvm(properties); javaSpec = new JavaSpec(properties); jvmSpec = new JvmSpec(properties); javaRuntime = new JavaRuntime(properties); origin.keySet().removeAll(properties.keySet()); usedProperties = new HashSet<String>(origin.keySet()); }
public void testComposite() { RuleExecutorBuilder builder = (DefaultRuleExecutorBuilder) applicationContext.getBean("ruleExecutorBuilder"); List<Rule> rules = CollectUtils.newArrayList(); // Rule rule1 = (Rule) Model.newInstance(Rule.class); // rule1.setFactory(DefaultRuleExecutorBuilder.SPRING); // rule1.setServiceName("ruleExecutor1"); Rule rule2 = new RuleBean(); rule2.setFactory(DefaultRuleExecutorBuilder.BEAN); rule2.setServiceName("org.beangle.rule.impl.RuleExecutor2"); // rules.add(rule1); rules.add(rule2); Context context = new SimpleContext(); RuleExecutor exceutor = builder.build(rules, false); exceutor.execute(context); }
Host() { try { InetAddress localhost = InetAddress.getLocalHost(); hostname = localhost.getHostName(); } catch (UnknownHostException e) { hostname = "localhost"; } // IPs try { for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) { NetworkInterface networkInterface = e.nextElement(); String name = networkInterface.getDisplayName(); List<String> addrs = CollectUtils.newArrayList(); for (Enumeration<InetAddress> e2 = networkInterface.getInetAddresses(); e2.hasMoreElements(); ) { addrs.add(e2.nextElement().getHostAddress()); } addresses.put(name, addrs); } } catch (Exception e) { } }
public static final class Host { String hostname; final Map<String, List<String>> addresses = CollectUtils.newHashMap(); Host() { try { InetAddress localhost = InetAddress.getLocalHost(); hostname = localhost.getHostName(); } catch (UnknownHostException e) { hostname = "localhost"; } // IPs try { for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) { NetworkInterface networkInterface = e.nextElement(); String name = networkInterface.getDisplayName(); List<String> addrs = CollectUtils.newArrayList(); for (Enumeration<InetAddress> e2 = networkInterface.getInetAddresses(); e2.hasMoreElements(); ) { addrs.add(e2.nextElement().getHostAddress()); } addresses.put(name, addrs); } } catch (Exception e) { } } public String getHostname() { return hostname; } public Map<String, List<String>> getAddresses() { return addresses; } }
/** * 规则 * * @author chaostone */ @Entity(name = "org.beangle.ems.rule.Rule") @Cacheable @Cache(region = "beangle", usage = CacheConcurrencyStrategy.READ_WRITE) public class RuleBean extends LongIdTimeObject implements Rule { private static final long serialVersionUID = -3648535746761474692L; /** 规则名称 */ @NotNull @Size(max = 100) @Column(unique = true) private String name; /** 适用业务 */ @NotNull @Size(max = 100) private String business; /** 规则描述 */ @NotNull @Size(max = 300) private String description; /** 规则管理容器 */ @NotNull @Size(max = 50) private String factory; /** 规则服务名 */ @NotNull @Size(max = 80) private String serviceName; /** 规则参数集合 */ @OneToMany(mappedBy = "rule", cascade = CascadeType.ALL, orphanRemoval = true) @Cache(region = "beangle", usage = CacheConcurrencyStrategy.READ_WRITE) private Set<RuleParameter> params = CollectUtils.newHashSet(); /** 是否启用 */ @NotNull private boolean enabled; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBusiness() { return business; } public void setBusiness(String business) { this.business = business; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getFactory() { return factory; } public void setFactory(String factory) { this.factory = factory; } public String getServiceName() { return serviceName; } public void setServiceName(String serviceName) { this.serviceName = serviceName; } public Set<RuleParameter> getParams() { return params; } public void setParams(Set<RuleParameter> params) { this.params = params; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } }
public abstract class AbstractAvatarBase implements AvatarBase { private final Logger logger = LoggerFactory.getLogger(AbstractAvatarBase.class); // 支持的照片类型 protected Set<String> types = CollectUtils.newHashSet(); protected List<String> typeList = new ArrayList<String>(); protected boolean readOnly = false; protected static final String DEFAULT_AVATAR = "default.jpg"; { for (String name : new String[] {"jpg", "JPG", "jpeg", "png", "gif", "GIF"}) { types.add(name); typeList.add(name); } } public boolean containType(String type) { return types.contains(type); } public Set<String> getTypes() { return types; } public void setTypes(Set<String> types) { this.types = types; } public Avatar getDefaultAvatar() { return getAvatar(DEFAULT_AVATAR); } public boolean isReadOnly() { return readOnly; } public void setReadOnly(boolean readOnly) { this.readOnly = readOnly; } public int updateAvatarBatch(File zipFile) { String tmpPath = System.getProperty("java.io.tmpdir") + "/avatar/"; logger.debug("unzip avatar to {}", tmpPath); ZipUtils.unzip(zipFile, tmpPath); File tmpAvatar = new File(tmpPath); int count = updateFile(tmpAvatar); logger.debug("removing avatar tmp path {}", tmpPath); tmpAvatar.delete(); return count; } private int updateFile(File path) { int count = 0; if (path.isDirectory()) { String[] fileNames = path.list(); for (String fileName : fileNames) { File file = new File(path.getAbsolutePath() + "/" + fileName); if (file.isDirectory()) { count += updateFile(file); file.delete(); } else { String type = Strings.substringAfter(fileName, "."); boolean passed = containType(type); if (passed) { logger.debug("updating avatar by {}", file.getName()); updateAvatar(Strings.substringBefore(fileName, "."), file, type); count++; } file.delete(); } } } return count; } public abstract boolean updateAvatar(String name, File file, String type); }
/** 根据请求的实体,生成代码 */ @SuppressWarnings("unchecked") public String gen(CodeFixture fixture) { // 在必要时查找相应的生成脚本 String script = fixture.getScript(); CodeScript codeScript = null; if (null == script) { codeScript = getCodeScript(EntityUtils.getEntityClassName(fixture.getEntity().getClass())); if (null == codeScript) { return null; } script = codeScript.getScript(); try { String code = (String) PropertyUtils.getProperty(fixture.getEntity(), codeScript.getAttr()); if (isValidCode(code)) { return code; } } catch (Exception e) { throw new RuntimeException(e); } } int seqLength = -1; // 替换自动代码生成中的seq[x] if (Strings.contains(script, SEQ)) { seqLength = Numbers.toInt(Strings.substringBetween(script, SEQ + "[", "]")); script = Strings.replace( script, SEQ + "[" + Strings.substringBetween(script, SEQ + "[", "]") + "]", SEQ); } fixture.setScript(script); String code = super.gen(fixture); List<String> seqs = CollectUtils.newArrayList(); if (-1 != seqLength) { try { OqlBuilder<?> builder = OqlBuilder.from(Class.forName(codeScript.getCodeClassName()), "entity"); builder.select( "select substr(entity." + codeScript.getAttr() + "," + (code.indexOf(SEQ) + 1) + "," + seqLength + ")"); builder.where( " entity." + codeScript.getAttr() + " like :codeExample", Strings.replace(code, SEQ, "%")); builder.where( "length(entity." + codeScript.getAttr() + ")=" + (code.length() - SEQ.length() + seqLength)); seqs = (List<String>) entityDao.search(builder); Collections.sort(seqs); } catch (Exception e) { throw new RuntimeException(e); } synchronized (this) { int newSeqNo = 0; for (Iterator<String> iter = seqs.iterator(); iter.hasNext(); ) { String seqNo = iter.next(); if (Numbers.toInt(seqNo) - newSeqNo >= 2) { break; } else { newSeqNo = Numbers.toInt(seqNo); } } newSeqNo++; String seqNo = String.valueOf(newSeqNo); if (0 != seqLength) { seqNo = Strings.repeat("0", seqLength - seqNo.length()) + newSeqNo; } code = Strings.replace(code, SEQ, seqNo); } } return code; }