Skip to content

Instantly share code, notes, and snippets.

View migue's full-sized avatar

Miguel Ángel Pastor Olivar migue

View GitHub Profile
@migue
migue / gist:4690167
Last active December 12, 2015 01:18
Small script to choose a random element in a list. It is used to raffle conference tickets among the MadridJUG community members
import scala.util.Random
def winner(members:List[String]):String = {
new Random(System.currentTimeMillis()).shuffle(members).head
}
// write the names of all the members involved in the raffle
val qcon = List("Jorge Baez", "Daniel Jiménez", "Gonzalo Gómez", "Jorge Nota", "Jose Ignacio Dominguez", "Daniel Hernández", "Manuel Carrasco")
@migue
migue / Metric.java
Last active December 15, 2015 19:19
/**
* Domain model representing the metrics of our system. All the metrics at our
* system should implement this interface
*
* @author migue
*
*/
public interface Metric {
public String name();
...
void activate(ComponentContext componentContext) {
System.out.println("Starting the metrics manager. Waiting for metrics providers . . .");
_executorService.execute(new Runnable() {
@Override
public void run() {
/**
* SPI for metrics extensions
*/
public interface MetricsProvider {
public Metric collect();
public Map<String, Object> getMetricProperties();
public Long getId();
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="metrics" immediate="true">
<scr:implementation class="com.github.migue.metrics.internal.manager.MetricsManager" />
<scr:reference
interface="com.github.migue.metrics.spi.MetricsProvider"
cardinality="0..n"
policy="dynamic"
bind="addMetricsProvider"
unbind="removeMetricsProvider"/>
public class JVMMetric implements Metric {
@Override
public String name() {
return "[JVM-Metric]";
}
@Override
public String display() {
StringBuffer sb = new StringBuffer(4);
<component
name="com.github.migue.metrics.jvm.internal.extension.JVMMetricsProvider"
immediate="true">
<implementation
class="com.github.migue.metrics.jvm.internal.extension.JVMMetricsProvider" />
<property name="service.description" value="JVM Metrics provider service" />
<property name="service.vendor" value="Migue" />
<service>
<provide interface="com.github.migue.metrics.spi.MetricsProvider" />
</service>
@migue
migue / ListUsersCommand.java
Created May 14, 2013 22:27
Basic OSGi Gogo shell consuming a Liferay service
import java.util.List;
import com.liferay.gogo.commands.user.internal.AbstractUserManagementCommand;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalService;
import aQute.bnd.annotation.component.Component;
import aQute.bnd.annotation.component.Reference;
@migue
migue / TemplateTracker.java
Last active December 31, 2015 15:28
Sample extension mechanism for Liferay using the OSGi capabilities
public class TemplateTracker implements BundleTrackerCustomizer {
@Override
public Object addingBundle(Bundle bundle, BundleEvent event) {
// lets track all the bundles publishing the _TEMPLATES_LOCATION header
String templatesLocation = bundle.getHeaders().get(_TEMPLATES_LOCATION);
if (templatesLocation == null) {