Skip to content

Instantly share code, notes, and snippets.

@eclecticlogic
eclecticlogic / CubicBezierCurve.java
Last active May 11, 2020 00:19
Fits a Cubic Bezier curve and allows you to treat it as y = f(x)
import java.util.HashMap;
import java.util.Map;
/**
* @author kabram.
*
*/
public class CubicBezierCurve {
@eclecticlogic
eclecticlogic / DependencyInjector.java
Last active August 29, 2015 14:10
Spring runtime dependency injector
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
public class DependencyInjector {
@Inject
private AutowiredAnnotationBeanPostProcessor aaProcessor;
@eclecticlogic
eclecticlogic / DSLExecutor.groovy
Created September 26, 2014 17:51
Groovy DSL executor using supplied handler
class DSLExecutor {
/**
* @param filename Groovy file to execute.
* @param handler Handler that provides implementations for methos and properties the script references.
* @return A map of variables returned. The keys are the names of the variables.
*/
static Map<String, Object> execute(String filename, Object handler) {
ResourceLoader loader = new DefaultResourceLoader();
String scriptSource = loader.getResource(filename).getInputStream().getText()
@eclecticlogic
eclecticlogic / build.resolutionStrategy.gradle
Created August 10, 2014 00:29
Gradle resolutionStrategy
configurations.all {
resolutionStrategy {
// fail eagerly on version conflict (includes transitive dependencies)
// e.g. multiple different versions of the same dependency (group and name are equal)
failOnVersionConflict()
// force certain versions of dependencies (including transitive)
// *append new forced modules:
force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4'
// *replace existing forced modules with new ones:
@eclecticlogic
eclecticlogic / JettyBootstrap.java
Last active October 31, 2016 03:20
Creating a Jetty server with public and private connectors and setting up Jersey as a public servlet and one private servlet. Also bootsrap Spring. All this done without web.xml
public class JettyBootstrap {
private static Logger logger = LoggerFactory.getLogger(JettyBootstrap.class);
private String bindAddress;
private int port;
public void start() {
@eclecticlogic
eclecticlogic / CertificateInstaller.java
Created January 26, 2014 20:33
Installing self-signed and other X509 certification into cacerts in Java
/*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
@eclecticlogic
eclecticlogic / SVGToBufferedImage
Created December 10, 2013 13:05
Java code to convert an SVG into a BufferedImage (PNG, JPEG etc.) using Apache Batik
public BufferedImage createImageFromSVG(String svg) {
Reader reader = new BufferedReader(new StringReader(svg));
TranscoderInput svgImage = new TranscoderInput(reader);
BufferedImageTranscoder transcoder = new BufferedImageTranscoder();
transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) component.getWidth());
transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) component.getHeight());
try {
transcoder.transcode(svgImage, null);
} catch (TranscoderException e) {
@eclecticlogic
eclecticlogic / SpringClasspathScanner.java
Created November 19, 2013 20:04
Scan classpath to get classes matching specific attributes (type, package, etc).
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
// Filter to include only classes that have a particular annotation.
provider.addIncludeFilter(new AnnotationTypeFilter(MyAnnotation.class));
// Find classes in the given package (or subpackages)
Set<BeanDefinition> beans = provider.findCandidateComponents("com.xyz.abc");
for (BeanDefinition bd : beans) {
// The BeanDefinition class gives access to the Class<?> and other attributes.
}