<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>all-IT &#187; Apache commons</title>
	<atom:link href="http://www.k-oo.de/blog/category/apache-commons/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.k-oo.de/blog</link>
	<description>all-IT ist ein Blog aus dem IT Alltag</description>
	<lastBuildDate>Fri, 03 Feb 2012 09:42:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java Mail mit Apache commons</title>
		<link>http://www.k-oo.de/blog/2007/06/28/java-mail-mit-apache-commons/</link>
		<comments>http://www.k-oo.de/blog/2007/06/28/java-mail-mit-apache-commons/#comments</comments>
		<pubDate>Thu, 28 Jun 2007 08:10:34 +0000</pubDate>
		<dc:creator>gklinkmann</dc:creator>
				<category><![CDATA[Apache commons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://www.k-oo.de/blog/2007/06/28/java-mail-mit-apache-commons/</guid>
		<description><![CDATA[Mit Java Mails zu versenden ist nicht schwer, einen besonders einfachen Ansatz wählt das Apache Commons Projekt mit seinem Subprojekt Email. Das einzige, was man beachten muss sind die Abhängigkeiten zu Suns Java Mail und Activation Framework, deren Java Archive man mit in den CLASSPATH aufnehmen muss. Eine Klasse, die Commons Email verwendet und den [...]]]></description>
			<content:encoded><![CDATA[<p>Mit Java Mails zu versenden ist nicht schwer, einen besonders einfachen Ansatz wählt das <strong><a href="http://jakarta.apache.org/commons/">Apache Commons Projekt</a></strong> mit seinem <a href="http://jakarta.apache.org/commons/email/index.html">Subprojekt Email</a>. Das einzige, was man beachten muss sind die Abhängigkeiten zu <strong>Suns</strong> <a href="http://java.sun.com/products/javamail/">Java Mail</a> und <a href="http://java.sun.com/products/javabeans/glasgow/jaf.html">Activation Framework</a>, deren Java Archive man mit in den CLASSPATH aufnehmen muss.<br />
Eine Klasse, die Commons Email verwendet und den Versand von Emails mit Anhängen ermöglicht, könnte so aussehen (Mailserver und Port müssen natürlich angepasst werden):<br />
<span id="more-89"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> de.<span style="color: #006633;">k</span><span style="color: #339933;">-</span>oo.<span style="color: #006633;">mail</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.EmailAttachment</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.EmailException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.MultiPartEmail</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Mailer <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> subject,msg<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> to<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>EmailAttachment<span style="color: #339933;">&gt;</span> attachements<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> server<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;mailserver&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> port<span style="color: #339933;">=</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Mailer<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> subject, <span style="color: #003399;">String</span> msg, Map<span style="color: #339933;">&lt;</span>String
               , String<span style="color: #339933;">&gt;</span> to, List<span style="color: #339933;">&lt;</span>EmailAttachment<span style="color: #339933;">&gt;</span> attachements<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">subject</span> <span style="color: #339933;">=</span> subject<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">msg</span> <span style="color: #339933;">=</span> msg<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">to</span> <span style="color: #339933;">=</span> to<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">attachements</span> <span style="color: #339933;">=</span> attachements<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Mailer<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> subject, <span style="color: #003399;">String</span> msg, Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> to<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">subject</span> <span style="color: #339933;">=</span> subject<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">msg</span> <span style="color: #339933;">=</span> msg<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">to</span> <span style="color: #339933;">=</span> to<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">attachements</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>EmailAttachment<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>EmailAttachment<span style="color: #339933;">&gt;</span> getAttachements<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> attachements<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAttachements<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>EmailAttachment<span style="color: #339933;">&gt;</span> attachements<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">attachements</span> <span style="color: #339933;">=</span> attachements<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> msg<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMsg<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">msg</span> <span style="color: #339933;">=</span> msg<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getSubject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> subject<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSubject<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> subject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">subject</span> <span style="color: #339933;">=</span> subject<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> getTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> to<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTo<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> to<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">to</span> <span style="color: #339933;">=</span> to<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> mail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> EmailException <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Create the email message</span>
		MultiPartEmail email <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MultiPartEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		email.<span style="color: #006633;">setHostName</span><span style="color: #009900;">&#40;</span>server<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		email.<span style="color: #006633;">setSmtpPort</span><span style="color: #009900;">&#40;</span>port<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		email.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;prosys@zid-f.bfinv.de&quot;</span>, <span style="color: #0000ff;">&quot;prosys&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> to.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Mail Adresse, Alias</span>
			email.<span style="color: #006633;">addTo</span><span style="color: #009900;">&#40;</span>key, to.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		email.<span style="color: #006633;">setSubject</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		email.<span style="color: #006633;">setMsg</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Create the attachment</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>EmailAttachment attachment <span style="color: #339933;">:</span> attachements<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// add the attachment</span>
			email.<span style="color: #006633;">attach</span><span style="color: #009900;">&#40;</span>attachment<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// send the email</span>
		email.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

 <p><a href="http://www.k-oo.de/blog/?flattrss_redirect&amp;id=89&amp;md5=fdfb4fe611755c71fafd2d31e0944c33" title="Flattr" target="_blank"><img src="http://www.k-oo.de/blog/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.k-oo.de/blog/2007/06/28/java-mail-mit-apache-commons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.k-oo.de/blog/?flattrss_redirect&amp;id=89&amp;md5=fdfb4fe611755c71fafd2d31e0944c33" type="text/html" />
	</item>
		<item>
		<title>Java und die Kommandozeile</title>
		<link>http://www.k-oo.de/blog/2006/12/01/java-und-die-kommandozeile/</link>
		<comments>http://www.k-oo.de/blog/2006/12/01/java-und-die-kommandozeile/#comments</comments>
		<pubDate>Fri, 01 Dec 2006 06:12:46 +0000</pubDate>
		<dc:creator>gklinkmann</dc:creator>
				<category><![CDATA[Apache commons]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://www.k-oo.de/blog/?p=5</guid>
		<description><![CDATA[Wer mit Java programmiert musste sicher schon einmal für eine Java Applikation die Kommandozeilenparameter auslesen. Wenn man nun nicht das Rad immer wieder neu erfinden will, sollte man zu erst einen Blick auf die Apache commons Bibliothek werfen. Tut man das, so wird man dort auch für das Kommandozeilenproblem fündig. Die Lösung heißt &#8211; Apache [...]]]></description>
			<content:encoded><![CDATA[<p>Wer mit Java programmiert  musste sicher schon einmal für eine Java Applikation  die  Kommandozeilenparameter auslesen.  Wenn man  nun nicht das Rad immer wieder neu erfinden will, sollte man zu erst einen Blick auf die <a target="_blank" href="http://jakarta.apache.org/commons">Apache commons Bibliothek</a> werfen.</p>
<p>Tut man das, so wird man dort auch für das Kommandozeilenproblem fündig. Die Lösung heißt &#8211; <a target="_blank" href="http://jakarta.apache.org/commons/cli/usage.html">Apache commons &#8211; CLI</a>. Die Bibliothek hilft nicht nur beim Auslesen der Kommandozeilenparameter, sondern bietet auch gleich die Möglichkeit eine Hilfe für die Verwendung der Parameter zu generieren.</p>
<p>Ein kleines Beispiel, sagt mehr als 1000 Worte<br />
1.) Installation</p>
<ul>
<li>downloaden und auspacken</li>
<li>commons-cli-1.0.jar in den CLASS_PATH aufnehmen</li>
</ul>
<p>2.)  Definition der Optionen</p>
<pre class="code">
<code>Options options = new Options();

Option help = new Option( "help", "print this message" );
Option optDescription = OptionBuilder.withArgName( "string" )
   .hasArg()
   .withDescription(  "use given description for incident" )
   .create( "description" );
Option optLongDescription = OptionBuilder.withArgName( "string" )
   .hasArg()
   .withDescription(  "use given description for incident" )
   .create( "longDescription" );

options.addOption(help);
options.addOption(optDescription);
options.addOption(optLongDescription);
</code>
</pre>
<p>3.) Parsen der Kommandozeilenargumente</p>
<pre class="code">
<code>CommandLineParser parser = new GnuParser();
CommandLine line = parser.parse( options, args );
</code>
</pre>
<p>4.) Auslesen von Optionen ohne Argumente</p>
<pre class="code">
<code>if(line.hasOption("h")) {
   // Ausgabe der Hilfe
}
</code>
</pre>
<p>5.) Auslesen von Optionen mit Argumenten</p>
<pre class="code">
<code>String description="";
String longDescription="";

if( line.hasOption( "description" ) ) {
   description = line.getOptionValue( "description" );
   log.debug("Description: "+description);
}

if( line.hasOption( "longDescription" ) ) {
   longDescription = line.getOptionValue( "longDescription" );
   log.debug("Long Description: "+longDescription);
}
</code>
</pre>
<p>6.) Ausgabe für die Hilfe generieren</p>
<pre class="code">
<code>HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "ant", options );
</code>
</pre>
<p>7.) fertig -> einfach schön oder schön einfach <img src='http://www.k-oo.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  </p>
 <p><a href="http://www.k-oo.de/blog/?flattrss_redirect&amp;id=5&amp;md5=98e7e0c6230bc4b755c89c0514343621" title="Flattr" target="_blank"><img src="http://www.k-oo.de/blog/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.k-oo.de/blog/2006/12/01/java-und-die-kommandozeile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<atom:link rel="payment" href="http://www.k-oo.de/blog/?flattrss_redirect&amp;id=5&amp;md5=98e7e0c6230bc4b755c89c0514343621" type="text/html" />
	</item>
	</channel>
</rss>

