<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Christian Rudh on Medium]]></title>
        <description><![CDATA[Stories by Christian Rudh on Medium]]></description>
        <link>https://medium.com/@crudh?source=rss-45802f855e79------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8LXYM93I-ESmHG5u.png</url>
            <title>Stories by Christian Rudh on Medium</title>
            <link>https://medium.com/@crudh?source=rss-45802f855e79------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:33:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@crudh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Custom event dispatchers in Apache Wicket, part 2]]></title>
            <link>https://medium.com/@crudh/custom-event-dispatchers-in-apache-wicket-part-2-148de18dd51c?source=rss-45802f855e79------2</link>
            <guid isPermaLink="false">https://medium.com/p/148de18dd51c</guid>
            <dc:creator><![CDATA[Christian Rudh]]></dc:creator>
            <pubDate>Sun, 31 Aug 2014 19:16:49 GMT</pubDate>
            <atom:updated>2014-08-31T19:18:05.310Z</atom:updated>
            <content:encoded><![CDATA[<p>In <a href="https://medium.com/@crudh/custom-event-dispatchers-in-apache-wicket-part-1-47f48997171c">part 1</a> we created our own custom event dispatcher for Wicket. In part 2 we will make it simpler to use for developers, faster and usable in more cases. The text in part 2 is written under the assumption that the reader has read <a href="https://medium.com/@crudh/custom-event-dispatchers-in-apache-wicket-part-1-47f48997171c">part 1</a>, so many things will not be explained here.</p><p>The main problem of our previous implementation is that we need to both annotate the class and the method. And also we need to specify the payload we are interested in for both annotations. There are several problems with this:</p><ul><li>If we forget the annotation on the class then the annotation on the method does nothing</li><li>If we forget a specific payload on the class annotation it will never reach the method handling it</li><li>In Wicket it is common to create anonymous inner classes, but since we require an annotation on the class we can’t use our event dispatcher for such classes</li></ul><p>So it seems like the obvious way is to remove the <em>@EventPayloadReceiver</em> annotation for the class and only keep the <em>@OnEventPayload</em> annotation for the method. But we added the class annotation to optimize event delivery, so we didn’t have to check each method in each component for every event delivered. So how do we keep everything running fast while removing the class annotation? Well by caching of course.</p><p>So what we will do is keep the <em>@OnEventPayload</em> as it is, delete the <em>@EventPayloadReceiver</em> and rewrite the event delivery in our dispatcher <em>AnnotationPayloadEventDispatcher</em>. All the code following is in our event dispatcher.</p><p>First we create a static Map to hold the cached data:</p><pre>private final static Map&lt;Class&lt;?&gt;, Map&lt;Class&lt;?&gt;, MethodInfo&gt;&gt; cacheMap;</pre><pre>static {<br>  cacheMap = new HashMap&lt;Class&lt;?&gt;, Map&lt;Class&lt;?&gt;, MethodInfo&gt;&gt;();<br>}</pre><p>The MethodInfo in the Map is a simple class that describes the basics needed to call a specific method:</p><pre>private static class MethodInfo {<br>  private final String name;<br>  private final Class[] parameterTypes;</pre><pre>  /**<br>   * @param name name of the method<br>   * @param parameterTypes the parameter type list of the method<br>   */<br>  private MethodInfo(String name, Class[] parameterTypes) {<br>    this.name = name;<br>    this.parameterTypes = parameterTypes;<br>  }<br>}</pre><p>Then we create a few simple helper methods for working with the cache Map:</p><pre>/**<br> * Checks the cache if a sink has been cached.<br> *<br> * @param sinkClass class of the sink<br> * @return true if cached<br> */<br>private boolean isSinkRegistered(Class sinkClass) {<br>  return cacheMap.containsKey(sinkClass);<br>}</pre><pre>/**<br> * Checks both that the sink has been cached and that it accepts a<br> * specific payload.<br> *<br> * @param sinkClass the class of the sink<br> * @param payloadClass the class of the payload<br> * @return true if both cached and it accepts the payload<br> */<br>private boolean isSinkRegisteredForPayload(Class sinkClass, Class payloadClass) {<br>  return isSinkRegistered(sinkClass) &amp;&amp; cacheMap.get(sinkClass).containsKey(payloadClass);<br>}</pre><pre>/**<br> * Gets info from the cache about a method that has the<br> * OnEventPayload annotation.<br> *<br> * @param sinkClass the sink that contains the method<br> * @param payloadClass the payload that we want the corresponding<br> *     method for<br> * @return info about the method */<br>private MethodInfo getMethodInfo(Class sinkClass, Class payloadClass) {<br>  return cacheMap.get(sinkClass).get(payloadClass);<br>}</pre><p>The next step is writing a helper method that does the actual caching. This method will cache all methods in a sink that are annotated with the <em>@OnEventPayload</em> annotation and the payloads it is configured to accept:</p><pre>private void cacheSink(Object sink) {<br>  Map&lt;Class&lt;?&gt;, MethodInfo&gt; payloadMethodMap = new HashMap&lt;Class&lt;?&gt;, MethodInfo&gt;();</pre><pre>  // Go through all methods and check for the annotation<br>  for (Method method : sink.getClass().getDeclaredMethods()) {<br>    if (method.isAnnotationPresent(OnEventPayload.class)) {<br>      OnEventPayload onEvent = method.getAnnotation(OnEventPayload.class);</pre><pre>      // Go through all payloads in the annotation and update the<br>      //  cache<br>      for (Class payloadClass : Arrays.asList(onEvent.value())) {<br>        if (!payloadMethodMap.containsKey(payloadClass)) {<br>          payloadMethodMap.put(payloadClass, new MethodInfo(method.getName(), method.getParameterTypes()));<br>        }<br>      }<br>    }<br>  }</pre><pre>  cacheMap.put(sink.getClass(), payloadMethodMap);<br>}</pre><p>Then we will write a helper method that handles the delivery of the event payload to a sink by using reflection:</p><pre>private void deliverEvent(Object sink, IEvent event, MethodInfo methodInfo) {<br>  try {<br>    // Get the method by using the MethodInfo data<br>    Method method = sink.getClass().getMethod(methodInfo.name, methodInfo.parameterTypes);</pre><pre>    // Make it accessible if it isn’t<br>    if (!method.isAccessible()) {<br>      method.setAccessible(true);<br>    }</pre><pre>    // Invoke the method and deliver the event payload<br>    method.invoke(sink, event.getPayload());<br>  } catch (Exception e) {<br>    throw new RuntimeException(“Exception when delivering event object “ + event.getPayload().getClass() + “ to component “ + sink.getClass() + “ and method “ + methodInfo.name, e);<br>  }<br>}</pre><p>Now we have all the helper methods we need and we can rewrite the main method, dispatchEvent, of the event dispatcher:</p><pre>@Override<br>public void dispatchEvent(Object sink, IEvent event, Component component) {<br>  // The payload must not be null<br>  if (event.getPayload() != null) {<br>    // Check that the sink is cached, otherwise scan it and add it<br>    //   to the cache<br>    if (!isSinkRegistered(sink.getClass())) {<br>      cacheSink(sink);<br>    }</pre><pre>    // Check if this sink accepts the specific payload, and deliver<br>    //   the event if it does<br>    if (isSinkRegisteredForPayload(sink.getClass(), event.getPayload().getClass())) {<br>      MethodInfo methodInfo = getMethodInfo(sink.getClass(), event.getPayload().getClass());<br>      deliverEvent(sink, event, methodInfo);<br>    }<br>  }<br>}</pre><p>And that is it. Now we have an implementation with only one annotation and one event dispatcher which makes using it much simpler. On top of that we have a cache, that is shared among all users, which will make it faster and we can use this event system in anonymous inner classes. So the benefits are many and the only real drawback is that we have a little bit more code in the event dispatcher.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=148de18dd51c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Custom event dispatchers in Apache Wicket, part 1]]></title>
            <link>https://medium.com/@crudh/custom-event-dispatchers-in-apache-wicket-part-1-47f48997171c?source=rss-45802f855e79------2</link>
            <guid isPermaLink="false">https://medium.com/p/47f48997171c</guid>
            <dc:creator><![CDATA[Christian Rudh]]></dc:creator>
            <pubDate>Sun, 31 Aug 2014 19:15:26 GMT</pubDate>
            <atom:updated>2014-08-31T19:15:26.569Z</atom:updated>
            <content:encoded><![CDATA[<p>Since version 1.5 of <a href="http://wicket.apache.org/">Wicket</a> there is a way for components to send and listen to events, called inter-component events. You can read about the basics <a href="http://wicket.apache.org/2011/09/07/wicket-1.5-released.html">here</a>. This is a very powerful and useful system, especially for ajax heavy pages where an action in one component can trigger a change in another without any coupling or without them even knowing of each other.</p><p>The way it works is that the sender implements the <a href="http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/event/IEventSource.html">IEventSource</a> interface and the receiver implements the <a href="http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/event/IEventSink.html">IEventSink</a> interface. All Wicket components does this already. The sender then uses the following to send an event, as an example:</p><pre>send(getPage(), Broadcast.BREADTH, myPayloadObject);</pre><p>So the event is sent to the page and then broadcasted to components according to breadth first traversal. The payload is the object we want to send to the other component(s). Now the receiver has to override the following method:</p><pre>void onEvent(IEvent event);</pre><p>All events will now be delivered to the method, but this specific component might only be interested in one type. So we have to do something like this:</p><pre>@Override<br>public onEvent(IEvent event) {<br>  if (event instanceof MyPayloadObject) {<br>    // do the work<br>  }<br>}</pre><p>So that is it, we now have inter-component events working. But this is only the standard way of doing it. The Wicket devs have allowed us to implement and register our own event dispatchers that can work in other ways. So below is an example of another way of doing it.</p><p>The first thing we do before we start hacking away at our new event dispatcher is to set up a few requirements:</p><ul><li>The only thing we are interested in is the payload, we don’t care about the IEvent</li><li>We should not have to use instanceof to check what event we have received</li><li>We should be able to have multiple methods in a component that receives different types of events</li><li>The event dispatcher should not have to check every method in every component for a method that accepts a specific event</li><li>We should be able to control it with annotations</li></ul><p>So we begin by creating an annotation that we can place on a method in a component to indicate that we want to receive events:</p><pre>@Retention(RetentionPolicy.RUNTIME)<br>@Target(ElementType.METHOD)<br>public @interface OnEventPayload {<br>  Class[] value() default { };<br>}</pre><p>Then we create an annotation that we place on the component itself:</p><pre>@Retention(RetentionPolicy.RUNTIME)<br>@Target(ElementType.TYPE)<br>public @interface EventPayloadReceiver {<br>  Class[] value() default { };<br>}</pre><p>And this is an example on how we can use them:</p><pre>@EventPayloadReceiver({MyPayloadObject1.class, MyPayloadObject2.class})<br>public class MyPanel extends Panel {<br>  // normal content here</pre><pre>  @OnEventPayload(MyPayloadObject1.class)<br>  public void onMyPayloadObject1(MyPayloadObject1 payload) {<br>    // do the work<br>  }</pre><pre>  @OnEventPayload(MyPayloadObject2.class)<br>  public void onMyPayloadObject2(MyPayloadObject2 payload) {<br>    // do the work<br>  }<br>}</pre><p>So the component indicates, by using @EventPayloadReceiver, that it can take two types of events. Our event dispatcher sees this and if the event it is delivering is matching any of those it can then check the annotated methods. It then selecteds the method that has the @OnEventPayload annotation and the matching payload object. This means that all of our requirements are met.</p><p>This example could have been even simpler by removing the @EventPayloadReceiver annotation and check all methods in all components that the event is broadcasted to. But then the dispatcher has to check, if we take the example above, all the methods with the @OnEventPayload annoation if it receives an event of type MyPayloadObject3 only to realize that there is no method for it. This might be ok if you have a few events and not many components at a given time, but with many events, components and methods it seems a bit wasteful.</p><p>The @OnEventPayload annotation also allows multiple events for one method if you want to do the same thing for multiple events. But then you should probably have all your payloads exend the same base class or you would have to take Object as a parameter for the method. This could be useful, for example, when the only thing the component should to is target itself with the ajax request when any of a number of events is delivered.</p><p>So now we have the logic of the event dispatcher. This is a simple example that could fulfill our requirements (see the inline comments for details):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a76b219f4a5e50a81bc7694e956ad82/href">https://medium.com/media/7a76b219f4a5e50a81bc7694e956ad82/href</a></iframe><p>And the final step is to register the the event dispatcher in the Wicket application, in the init method:</p><pre>getFrameworkSettings().add(new MyPayloadEventDispatcher());</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47f48997171c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Troubleshooting SELinux]]></title>
            <link>https://medium.com/@crudh/troubleshooting-selinux-2638c856171e?source=rss-45802f855e79------2</link>
            <guid isPermaLink="false">https://medium.com/p/2638c856171e</guid>
            <dc:creator><![CDATA[Christian Rudh]]></dc:creator>
            <pubDate>Sun, 31 Aug 2014 19:14:44 GMT</pubDate>
            <atom:updated>2014-08-31T19:14:44.968Z</atom:updated>
            <content:encoded><![CDATA[<p>To host many of my things I use <a href="http://centos.org/">CentOS</a> with <a href="http://selinuxproject.org/">SELinux</a> enabled. SELinux is nice because of the security advantages it brings, but it can be a pain to work with when setting up new things. This example is based on when I configured <a href="http://en.wikipedia.org/wiki/Cron">cron</a> to take backups of my<a href="http://www.mongodb.org/">mongodb</a> instance and mail them to me every night. But the process is pretty much the same no matter what it is that SELinux is blocking.</p><p>To mail I used <a href="http://en.wikipedia.org/wiki/Mailx">mailx</a> that is a part of most Linux systems, and this is where SELinux stopped me. First it didn’t allow cron to send mail and secondly it didn’t allow mailx to read the file I wanted to attach. So this is how i tracked down what was happening and how i told SELinux to allow these things.</p><p>This is how it can look, just a non working example:</p><pre>$ mailx …. <br>Permission denied on the attachment file</pre><p>To see if SELinux is the culprit we look in the audit.log, the log belonging to SELinux. And we only check for things related to mailx:</p><pre>$ cat /var/log/audit/audit.log | grep mailx<br>type=AVC msg=audit(1353724381.477:92977): avc: denied { read } for pid=5257 comm=”mailx” …<br>type=AVC msg=audit(1353809346.126:96169): avc: denied { open } for pid=8465 comm=”mailx” …<br>type=AVC msg=audit(1353895922.113:104567): avc: denied { getattr } for pid=12760 comm=”mailx” …</pre><p>So it is SELinux that is preventing us from sending the mail. To fix this we first make sure we have the “policycoreutils-python” package installed. This contains the tool “audit2allow” that we will be using to create policy files from the entries in the audit log.</p><pre>$ yum install policycoreutils-python</pre><p>Then we generate a policy that we can check:</p><pre>$ cat /var/log/audit/audit.log | grep mailx | grep denied | audit2allow -m cronmailx &gt; cronmailx.te</pre><p>We check it and, in this example, it allows us to open and read the attachment file:</p><pre>$ cat cronmailx.te<br>module cronmailx 1.0;</pre><pre>require {<br>type var_t;<br>type system_mail_t;<br>class file { read getattr open };<br>}</pre><pre>#============= system_mail_t ==============<br>allow system_mail_t var_t:file getattr;<br>allow system_mail_t var_t:file { read open };</pre><p>And at last we generate a policy we can load and load it:</p><pre>$ cat /var/log/audit/audit.log | grep mailx | grep denied | audit2allow -M cronmailx<br>$ semodule -i cronmailx.pp</pre><p>Now either everything works or there are more SELinux rules that stops us (that we didn’t reach earlier). If there are more then just repeat the process until it works.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2638c856171e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading the POM version in a web application]]></title>
            <link>https://medium.com/@crudh/reading-the-pom-version-in-a-web-application-69b3001c3a0e?source=rss-45802f855e79------2</link>
            <guid isPermaLink="false">https://medium.com/p/69b3001c3a0e</guid>
            <dc:creator><![CDATA[Christian Rudh]]></dc:creator>
            <pubDate>Sun, 31 Aug 2014 19:14:05 GMT</pubDate>
            <atom:updated>2014-08-31T19:18:26.555Z</atom:updated>
            <content:encoded><![CDATA[<p>Sometimes you need to get the version of your application in the Java code. Setting a constant manually is an extra step in the release process and will probably be forgotten in some releases.</p><p>But there is a simple way if you are using Maven: You can tell the war plugin to put the application version into the MANIFEST.MF file. From there you can read it in your Java code. Here is an example for configuring the plugin:</p><pre>&lt;plugin&gt;<br>  &lt;groupId&gt;org.apache.maven.plugins&lt;groupId&gt;<br>  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;<br>  &lt;version&gt;2.1.1&lt;/version&gt;<br>  &lt;configuration&gt;<br>    &lt;archive&gt;<br>      &lt;manifest&gt;<br><strong>&lt;addDefaultImplementationEntries&gt;true&lt;/addDefaultImplementationEntries&gt;<br>      </strong>&lt;/manifest&gt;<br>    &lt;/archive&gt;<br>  &lt;/configuration&gt;<br>&lt;/plugin&gt;</pre><p>The important part is to configure the plugin with the <em>addDefaultImplementationEntries </em>directive. This will tell it to put the version, among other things, in the MANIFEST.MF file.</p><p>Then we read the version in the Java code. Below is a simple example:</p><pre>ServletContext servletContext = <strong>getServletContext()</strong>;<br>InputStream inputStream = servletContext.getResourceAsStream(“/META-INF/MANIFEST.MF”);<br>Manifest manifest = new Manifest(inputStream);<br>String applicationVersion = manifest.getMainAttributes().getValue(“Implementation-Version”); </pre><p>The <em>getServletContext() </em>method should be replaced with the regular way of getting the servlet context in the web framework you use. And the example lacks try-catch and closing of the inputStream.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69b3001c3a0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating an RSS feed using Scala and Play]]></title>
            <link>https://medium.com/@crudh/creating-an-rss-feed-using-scala-and-play-3146b7410ebd?source=rss-45802f855e79------2</link>
            <guid isPermaLink="false">https://medium.com/p/3146b7410ebd</guid>
            <dc:creator><![CDATA[Christian Rudh]]></dc:creator>
            <pubDate>Sun, 31 Aug 2014 19:13:26 GMT</pubDate>
            <atom:updated>2014-08-31T19:13:26.223Z</atom:updated>
            <content:encoded><![CDATA[<p>In previous Java projects I have used the <a href="http://rometools.org/">ROME</a> utility to create an <a href="http://en.wikipedia.org/wiki/RSS">RSS</a> feed and it has worked fine. So my first thought when I needed to create a feed in <a href="http://www.scala-lang.org/">Scala</a> and <a href="http://www.playframework.org/">Play</a> was if I should use ROME again or if there was a native Scala alternative. I googled a bit but didn’t find anything obvious.</p><p>But since I only needed a very basic feed and I knew that Scala has excellent build in XML syntax I decided to roll it myself. And it couldn’t be simpler, both thanks to Scala and the simplicity of the Play framework.</p><p>Below is a code example for a Play Action that serves a feed. “NewsArticle.all” fetches the articles and then we loop them and output the title, text and link to the article. And that’s all there is to it, without any external dependencies.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1bbf50658ca40439397d440d9301ed2/href">https://medium.com/media/c1bbf50658ca40439397d440d9301ed2/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3146b7410ebd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coherence cluster and cache info]]></title>
            <link>https://medium.com/@crudh/coherence-cluster-and-cache-info-5323976b0166?source=rss-45802f855e79------2</link>
            <guid isPermaLink="false">https://medium.com/p/5323976b0166</guid>
            <dc:creator><![CDATA[Christian Rudh]]></dc:creator>
            <pubDate>Sun, 31 Aug 2014 19:12:31 GMT</pubDate>
            <atom:updated>2014-08-31T19:12:31.728Z</atom:updated>
            <content:encoded><![CDATA[<p>Here is a code snippet that is useful if you have an application that is using Oracle Coherence for caching. It will print info about all the members in the cluster and the active caches.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c6992e61450ef107a6ec673cf8d832b/href">https://medium.com/media/6c6992e61450ef107a6ec673cf8d832b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5323976b0166" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>