<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://nightspawn.com/</id>
  <title>blog feed</title>
  <updated>2012-08-30T00:59:02Z</updated>
  <link rel="alternate" href="http://nightspawn.com/"/>
  <link rel="self" href="http://nightspawn.com/rants/feed.xml"/>
  <author>
    <name>nsn</name>
    <uri>http://nightspawn.com</uri>
  </author>
  <entry>
    <id>tag:nightspawn.com,2012-08-30:/rants/jersey-client-generic-types/</id>
    <title type="html">Consuming @GenericType REST-webservices with jersey-client</title>
    <published>2012-08-30T00:59:02Z</published>
    <updated>2012-08-30T00:59:02Z</updated>
    <author>
      <name>nsn</name>
      <uri>http://nightspawn.com</uri>
    </author>
    <link rel="alternate" href="http://nightspawn.com/rants/jersey-client-generic-types/"/>
    <content type="html">&lt;p&gt;… but jersey always complained about a missing message body reader:&lt;/p&gt;

&lt;pre class="brush: plain"&gt;
SEVERE: A message body reader for Java class java.util.List, 
and Java type java.util.List&amp;lt;MyClass&amp;gt;, 
and MIME media type application/json was not found 
&lt;/pre&gt;

&lt;p&gt;Turns out you have to add a matching provider to your &amp;lt;pre&amp;gt;com.sun.jersey.api.client.Client&amp;lt;/pre&amp;gt;:&lt;/p&gt;

&lt;pre class="brush: java"&gt;
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getClasses().add(JacksonJsonProvider.class);
client = Client.create(clientConfig);
WebResource r = client.resource(endpoint);
List&amp;lt;MyClass&amp;gt; list = r.accept(MediaType.APPLICATION_JSON).
    get(new GenericType&amp;lt;List&amp;amp;ltMyClass&amp;gt;&amp;gt;() {
  });
&lt;/pre&gt;

&lt;p&gt;Thanks to &lt;a href="http://stackoverflow.com/users/1278297/cobusbernard"&gt;cobusbernard&lt;/a&gt; on &lt;a href="http://stackoverflow.com"&gt;stackoverflow&lt;/a&gt; for that one.
I hope this’ll save somebody some time as it’s a bit hard to find.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:nightspawn.com,2011-11-30:/rants/cdi-alternatives-at-runtime/</id>
    <title type="html">choosing CDI @Alternatives at runtime</title>
    <published>2011-11-30T12:12:31Z</published>
    <updated>2011-11-30T12:12:31Z</updated>
    <author>
      <name>nsn</name>
      <uri>http://nightspawn.com</uri>
    </author>
    <link rel="alternate" href="http://nightspawn.com/rants/cdi-alternatives-at-runtime/"/>
    <content type="html">&lt;p&gt;I just could’nt believe I was the only person having this problem I searched the web for a solution, even made threads at the 
&lt;a href="http://seamframework.org/Community/CDIConfigurationAtRuntime"&gt;Weld Users Forum&lt;/a&gt; and  at 
&lt;a href="http://stackoverflow.com/questions/8137696/jsr-299-cdi-configuration-at-runtime"&gt;stackoverflow&lt;/a&gt;, but nobody was able to help me - so after sinking way to much time into
the JSR-299 spec and weld source code I came up with this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: we want to choose different sets of &lt;code&gt;@Alternative&lt;/code&gt;s depending on the environment our war file is deployed to. First thing we do is create an &lt;code&gt;@Alternative @Stereotype&lt;/code&gt; to
hold our meta-information and also in order to just add one type to the &lt;code&gt;beans.xml&lt;/code&gt; &lt;code&gt;&amp;lt;alternatives&amp;gt;&lt;/code&gt; section instead of adding each &lt;code&gt;@Alternative&lt;/code&gt; seperately:&lt;/p&gt;

&lt;pre class="brush: java"&gt;
@Alternative
@Stereotype
@Retention(RUNTIME)
@Target(TYPE)
public @interface EnvironmentAlternative {
    EnvironmentType[] value();
}
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;EnvironmentType&lt;/code&gt; is just an &lt;code&gt;enum&lt;/code&gt; detailing the different - wait for it… environment types:&lt;/p&gt;

&lt;pre class="brush: java"&gt;
public enum EnvironmentType {
    DEVELOPMENT, TESTING, STAGING, PRODUCTION;
}
&lt;/pre&gt;

&lt;p&gt;We need to make sure to have a single &lt;code&gt;@Default&lt;/code&gt; implementation for each interface, CDI will abort during deployment otherwise. All other implementations get annotated with our nnw
stereotype:&lt;/p&gt;

&lt;pre class="brush: java"&gt;
@Default
public class MockupFoo implements Foo {
  ...
}

@EnvironmentAlternative({STAGING, PRODUCTION})
public class FooImpl implements Foo {
  ...
}
&lt;/pre&gt;

&lt;p&gt;As &lt;code&gt;EnvironmentAlternative&lt;/code&gt; is an alternative stereotype &lt;code&gt;MockupFoo&lt;/code&gt; is now choosen by the CDI container for all injection points. After we add the following lines to our beans.xml
&lt;code&gt;FooImpl&lt;/code&gt; is choosen instead:&lt;/p&gt;

&lt;pre class="brush: xml"&gt;
&amp;lt;alternatives&amp;gt;
  &amp;lt;stereotype&amp;gt;com.example.EnvironmentAlternative&amp;lt;/stereotype&amp;gt;
&amp;lt;/alternatives&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Not exactly what we wanted, but there’s something we can do about that: ever heard of CDI extensions? They are a way to extend CDI (duh) by observing CDI lifecycle events. Writing one
is quite easy, just extend the &lt;code&gt;Extension&lt;/code&gt; interface:&lt;/p&gt;

&lt;script type="syntaxhighlighter" class="brush: java"&gt;&lt;![CDATA[
public class EnvironmentAlternativesExtension implements Extension {
    private EnvironmentType currentEnv = PRODUCTION;

    public &lt;T&gt; void processAnotated(@Observes ProcessAnnotatedType&lt;T&gt; event) {
        EnvironmentAlternative a = event.getAnnotatedType().getJavaClass().getAnnotation(EnvironmentAlternative.class);
        if (a != null &amp;&amp; !containsCurrentEnv(a.value())) {
            // veto if currentEnv not in type's target environments
            log.info("veto! " + event.getAnnotatedType().getJavaClass());
            event.veto();
        }
    }

    private boolean containsCurrentEnv(EnvironmentType[] environments) {
        for (EnvironmentType env : environments) {
            if (env == currentEnv) {
                return true;
            }
        }
        return false;
    }
}
]]&gt;&lt;/script&gt;

&lt;p&gt;The extension itself is not very complicated: the &lt;code&gt;processAnotated&lt;/code&gt; method gets called for each annotated type the container processes, and if it is annotated as &lt;code&gt;@EnvironmentAlternative&lt;/code&gt;
&lt;em&gt;AND&lt;/em&gt; the current environemnt is &lt;em&gt;NOT&lt;/em&gt; in the specified environments we call the event’s &lt;code&gt;veto()&lt;/code&gt; method, this prevents the type from being processed any further. Easy! The only thing left
to do is create a file called &lt;code&gt;javax.enterprise.inject.spi.Extension&lt;/code&gt; in your app’s  &lt;code&gt;META-INF/services&lt;/code&gt; directory containing a single line: our extension’s fully qualified classname.&lt;/p&gt;

&lt;p&gt;In a real application of course instead of hardcoding the &lt;code&gt;currentEnv&lt;/code&gt; field you’d have to decide how to obtain the current environment type, I just use a system property in our different glassfish
installations that I create with &lt;/p&gt;

&lt;pre&gt;
  nsn@nsn: ~&amp;gt; asadmin create-system-properties ENVIRONMENT=DEVELOPMENT
&lt;/pre&gt;

&lt;p&gt;and then simply obtain via&lt;/p&gt;

&lt;pre class="brush: java"&gt;
    String propertyValue = System.getProperty("ENVIRONMENT");
    currentEnv = EnvironmentType.valueOf(propertyValue);
&lt;/pre&gt;

&lt;p&gt;In our current project my team and I use this method to be able to build our application once, then deploy it to different environments and have it behave differently there. The only
problem I wasn’t able to solve with this technique is choosing which &lt;code&gt;@Decorators&lt;/code&gt; are enabled, as if I just &lt;code&gt;veto()&lt;/code&gt; a &lt;code&gt;@Decorator&lt;/code&gt; that is explicitly enabled in the beans.xml the container
complains about not being able to find that class. If anybody comes up with a solution to this please contact me somehow, for now our &lt;code&gt;@Decorator&lt;/code&gt;s themselves are responsible for when they 
are active and when not.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:nightspawn.com,2011-11-07:/rants/configuring-jee-apps-with-cdi/</id>
    <title type="html">configuring jee apps with cdi</title>
    <published>2011-11-07T13:32:51Z</published>
    <updated>2011-11-07T13:32:51Z</updated>
    <author>
      <name>nsn</name>
      <uri>http://nightspawn.com</uri>
    </author>
    <link rel="alternate" href="http://nightspawn.com/rants/configuring-jee-apps-with-cdi/"/>
    <content type="html">&lt;p&gt;&lt;strong&gt;Goal&lt;/strong&gt;: we want to maintain all our config params in the source code during development, but still be able to change them during compile- or even runtime later in the 
application’s lifespan. We will achieve this by using JSR-299 dependency injection:&lt;/p&gt;

&lt;p&gt;First we create a &lt;code&gt;@Qualifier&lt;/code&gt; to label all our config params with:&lt;/p&gt;

&lt;pre class="brush: java"&gt;
@Target(value = { FIELD, METHOD })
@Retention(value = RUNTIME)
@Dependent
@Inherited
@Qualifier
public @interface ConfigValue {
}
&lt;/pre&gt;

&lt;p&gt;We’ll certainly need additional information about the config param: a symbolic name, a default value and a description about how the param is going to be used.
Unfortunately we can’t just add methods to our qualifier-interface as CDI matches producer methods exactly - we cannot use the qualifier to specify more meta-information.
So we have the choice to either create a seperate annotation for each piece of meta-information we want to be able to specify, or just create a single annotation type
and add all needed methods there. Since Java does not allow &lt;code&gt;null&lt;/code&gt; as &lt;code&gt;default&lt;/code&gt; values in annotations (somehow &lt;code&gt;null&lt;/code&gt; is not a ‘constant expression’, whatever…) I decided
to create one annotation for all mandatory data, and then create an annotation for each piece of optional information:&lt;/p&gt;

&lt;pre class="brush: java"&gt;
@Target(value = { FIELD, METHOD })
@Retention(value = RUNTIME)
@Inherited
public @interface ConfigParams {
    /**
     * the {@link ConfigValue}'s default value, always a String, if a non-String
     * {@link ConfigValue} is annotated with a preset that cannot be converted
     * an exception is thrown in the producer method and 'svn blame' is going to
     * be used against you
     */
    String preset();

    /**
     * describes what the {@link ConfigValue} is used for, at this specific
     * injection point
     */
    String purpose();
}

/**
  * specifies an optional symbolic name for a {@link ConfigValue}
  */
@Target(value = { FIELD })
@Retention(value = RUNTIME)
@Inherited
public @interface ConfigKey {
    /**
     * the symbolic name to be used
     */
    String value();
}
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;ConfigParams&lt;/code&gt; holds all mandatory information (default value and a short description on how the param is going to be used), 
while &lt;code&gt;ConfigKey&lt;/code&gt; is optional and specifies a symbolic name for a &lt;code&gt;ConfigValue&lt;/code&gt;. 
This is necessary because our producer service chooses the fully qualified classname and fieldname as a default name for a &lt;code&gt;ConfigValue&lt;/code&gt;,
and the &lt;code&gt;ConfigKey&lt;/code&gt; allows us to use the same &lt;code&gt;ConfigValue&lt;/code&gt; at different locations in our project.
Now we just need to write &lt;code&gt;@Produces&lt;/code&gt; methods for each annotated type, we’ll just implement Strings for now:&lt;/p&gt;

&lt;pre class="brush: java"&gt;
@ApplicationScoped
public class ConfigurationService {
    @Inject
    private ConfigStore store;

    @Produces
    @ConfigValue
    public String makeConfigString(InjectionPoint injectionPoint) throws ConfigMetaInfoException {
        // extract meta information
        ConfigParams params = injectionPoint.getAnnotated().getAnnotation(ConfigParams.class);
        if (params == null) {
            throw new ConfigMetaInfoException("ConfigParams annotation missing at injection point "
                    + injectionPoint.toString());
        }

        // key?
        String key = calcDefaultName(injectionPoint);
        ConfigKey cfgKey = injectionPoint.getAnnotated().getAnnotation(ConfigKey.class);
        if (cfgKey != null) {
            key = cfgKey.value();
        }

        // ask store for value
        String rv = store.getConfigValue(key, params.preset());
        // log.info(String.format("config %s =&amp;gt; %s", key, rv));

        return rv;
    }
}
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;ConfigStore&lt;/code&gt; interface defines a store for &lt;code&gt;@ConfigValues&lt;/code&gt;, in my current project this is a &lt;code&gt;@Singleton&lt;/code&gt; bean that uses
JPA to persist config params to a database. Let’s just use a Hashmap in memory to demonstrate:&lt;/p&gt;

&lt;pre class="brush: java"&gt;
/**
 * default {@link ConfigStore} implementation, stores config values locally in a
 * HashMap, does not persist changes anywhere but in memory
 * 
 * @author nsn
 * 
 */
@Dependent
@Default
public class MockupConfigStoreImpl implements ConfigStore {
    private HashMap&amp;lt;String, String&amp;gt; values;

    /**
     * initializes the values map
     */
    @PostConstruct
    public void init() {
        values = new HashMap&amp;lt;String, String&amp;gt;();
    }

    @Override
    public String getConfigValue(String key, String preset) {
        if (values.containsKey(key)) {
            return values.get(key);
        }
        values.put(key, preset);
        return preset;
    }

}
&lt;/pre&gt;

&lt;p&gt;A real &lt;code&gt;ConfigStore&lt;/code&gt; would probably need to provide a &lt;code&gt;getAllValues()&lt;/code&gt; method that returns all &lt;code&gt;ConfigValues&lt;/code&gt;, each with all their injection points, default value
and meaning meta-information. This method could then be used to create a simple web-frontend to actually set the config params.&lt;/p&gt;

&lt;p&gt;This is the way my team and I manage our current project’s configuration, and so far we like it a lot more than the .properties files we used for the
&lt;a href="http://wizard101.de"&gt;Wizard101 website&lt;/a&gt;.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:nightspawn.com,2011-11-06:/rants/good-riddance-php/</id>
    <title type="html">Good riddance PHP</title>
    <published>2011-11-06T03:22:21Z</published>
    <updated>2011-11-06T03:22:21Z</updated>
    <author>
      <name>nsn</name>
      <uri>http://nightspawn.com</uri>
    </author>
    <link rel="alternate" href="http://nightspawn.com/rants/good-riddance-php/"/>
    <content type="html">&lt;p&gt;For 8 years I was a PHP developer. I contributed to the &lt;a href="http://www.xp-framework.info/"&gt;XP-Framework&lt;/a&gt; and in my thesis I even 
&lt;a href="/tinkerings/turpitude/"&gt;integrated the Zend-Engine into the Java-VM&lt;/a&gt;. I know what I am talking about - and I say: don’t use PHP!. Ever.
Technologically PHP is at least 10 years behind the rest of the “web”-languages. The underlying Zend-Engine contains some of the worst C-code I’ve ever seen.
There are &lt;a href="http://www.reddit.com/r/lolphp"&gt;countless&lt;/a&gt; &lt;a href="http://phpsadness.com/"&gt;sites&lt;/a&gt; explaining in great detail why and where the language is broken. &lt;/p&gt;

&lt;p&gt;And I simply can not think of a single reason to chose PHP over any other language for “web programming”: Java, Ruby, Python - even Perl are more stable,
have better performance, better libraries and better communities to learn from.&lt;/p&gt;

&lt;p&gt;so: &lt;strong&gt;Good riddance PHP - and fuck you!&lt;/strong&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:nightspawn.com,2011-11-01:/rants/every-keyboard-sucks/</id>
    <title type="html">every keyboard sucks</title>
    <published>2011-11-01T15:12:22Z</published>
    <updated>2011-11-01T15:12:22Z</updated>
    <author>
      <name>nsn</name>
      <uri>http://nightspawn.com</uri>
    </author>
    <link rel="alternate" href="http://nightspawn.com/rants/every-keyboard-sucks/"/>
    <content type="html">&lt;p&gt;For the longest time now I typed on an excellent Enermax Aurora - it is very well made, flat (which is good for my wrists) and yet has good key resistence and feedback.
But one thing always bothered me: it doesn’t have a US key layout but rather some strange mix of US and UK - most notably it has a “large” return key, which displaces the
backspace and backslash/pipe keys. I’ve also grown to love the built-in USB hub.&lt;/p&gt;

&lt;p&gt;So when I now needed a new keyboard to replace the old, crappy and filthy standard cherry piece of crap at home 
I naturally wanted to buy the successor: &lt;a href="http://enermax.com/home.php?fn=eng/product_a1_1_1&amp;amp;lv0=3&amp;amp;lv1=12&amp;amp;no=65"&gt;the enermax Acrylux&lt;/a&gt;. 
Unfortunately it is &lt;em&gt;absolutely&lt;/em&gt; impossible to buy an Acrylux with en_US layout here in germany - damn! &lt;/p&gt;

&lt;p&gt;So I lamented for a while and then ordered the &lt;a href="http://www.cherry.de/cid/corded_keyboards_EASYHUB_Corded_MultiMedia_Keyboard.htm?"&gt;cherry easyhub&lt;/a&gt;.
It arrived, but the fact that the keys are huge, very close together and worst of all: aren’t beveled make it very difficult to type on, and my rate of error
skyrocketed. Also the keys have nothing on the Aurora’s and feel rather flimsy in comparison.&lt;/p&gt;

&lt;p&gt;I then ordered a &lt;a href="http://www.cherry.de/cid/corded_keyboards_STRAIT_BLACK_Corded_Keyboard.htm?"&gt;cherry STRAIT&lt;/a&gt;… &lt;em&gt;what a piece of crap&lt;/em&gt;!
It is built to look like the stylish apple keyboards (which I know almost nothing about), but falls short in all other areas:
it feels flimsy as hell and lacks feedback on whether you actually pressed a key or not - horrible! Also the insert/delete/home/end/… block is rotated 90 degrees - 
catastrophic for a vim-lover-yet-insert-lamer like me.&lt;/p&gt;

&lt;p&gt;So: does anybody know a flat keyboard with a decent feel to the keys that I can actually buy an american layout version of here in germany? I’d rather have a 
corded version, and a built-in USB-hub is a bonus. Any pointers?&lt;/p&gt;
</content>
  </entry>
</feed>
