blob: 1c09783434e8460afd2285929863e0d5085ecca0 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Embed Vysper into another application &mdash; Apache MINA</title>
<link href="/assets/css/common.css" rel="stylesheet" type="text/css"/>
<link href="/assets/css/vysper.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<script src="https://www.apachecon.com/event-images/snippet.js"></script>
<div id="container">
<div id="header">
<div id="subProjectsNavBar">
<a href="/">
Apache MINA Project
</a>
&nbsp;|&nbsp;
<a href="/mina-project/">
MINA
</a>
&nbsp;|&nbsp;
<a href="/asyncweb-project/">
AsyncWeb
</a>
&nbsp;|&nbsp;
<a href="/ftpserver-project/">
FtpServer
</a>
&nbsp;|&nbsp;
<a href="/sshd-project/">
SSHD
</a>
&nbsp;|&nbsp;
<a href="/vysper-project/">
<strong>Vysper</strong>
</a>
</div>
</div>
<div id="content">
<div id="leftColumn">
<div id="navigation">
<a class="acevent" data-format="wide" data-width="170"></a>
<h5>Social Networks</h5>
<ul>
<li><a href="https://fosstodon.org/@apachemina">Apache MINA Mastodon</a></li>
</ul>
<h5>Overview</h5>
<ul>
<li><a href="/vysper-project/index.html">Home</a> </li>
<li><a href="/vysper-project/documentation.html">Documentation</a></li>
<li><a href="/vysper-project/downloads.html">Downloads</a></li>
<li><a href="/vysper-project/sources.html">Source</a></li>
</ul>
<h5>Community</h5>
<ul>
<li><a href="/vysper-project/mailing_lists.html">Mailing Lists</a></li>
<li><a href="/vysper-project/getting_involved.html">Getting Involved</a></li>
<li><a href="/vysper-project/issue_tracking.html">Issue Tracking</a></li>
<li><a href="https://www.apache.org/foundation/contributing.html">Contributing</a></li>
<li><a href="https://www.apache.org/licenses/">License</a></li>
<li><a href="https://www.apache.org/security/">Security</a></li>
</ul>
<h5>Sponsorship</h5>
<ul>
<li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="https://www.apache.org/">apache.org</a></li>
</ul>
</div>
</div>
<div id="rightColumn">
<h1 id="embed-into-another-application">Embed into another application</h1>
<h2 id="about-logging">About logging</h2>
<p>Embedding software making use of logging into another one making use of logging itself can be cumbersome.
Vysper uses slf4j for logging, which foremost is a logging facade. The actual logging worker is then bound at runtime. It can be log4j, as in standalone Vysper, or some other logging framework. If you make use of one of these in your embedding application, just put the proper slf4j adapter lib onto the classpath. For more info about slf4j, see the website at slf4j.org.</p>
<h2 id="libraries">Libraries</h2>
<p>This example doesn&rsquo;t make use of the Spring-based deployment. So we don&rsquo;t need the Spring libraries here.</p>
<p>TODO: elaborate about the other libs needed.</p>
<h2 id="start-server-naivly">Start server (naivly)</h2>
<p>Having a Vysper XMPP server instance embedded into your own application is easy:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">import</span> <span style="color:#00f;font-weight:bold">org.apache.vysper.xmpp.server</span><span style="color:#666">;</span>
XMPPServer server <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> XMPPServer<span style="color:#666">(</span><span style="color:#b44">&#34;myembeddedjabber.com&#34;</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">start</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>Unfortunately, this is not the whole story, we need to do some more preparations first. Hang on!</p>
<h2 id="start-server-full-story">Start server (full story)</h2>
<p>We make use of the JCR-persistent storage, and we add one user.
If you need more users, add more. Just make sure their Jabber ID domain matches the server&rsquo;s domain (&ldquo;myembeddedjabber.com&rdquo;, in this case).</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#080;font-style:italic">// choose the storage you want to use
</span><span style="color:#080;font-style:italic"></span>StorageProviderRegistry providerRegistry <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> JcrStorageProviderRegistry<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#080;font-style:italic">//StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry();
</span><span style="color:#080;font-style:italic"></span>
<span style="color:#a2f;font-weight:bold">final</span> AccountManagement accountManagement <span style="color:#666">=</span> <span style="color:#666">(</span>AccountManagement<span style="color:#666">)</span> providerRegistry<span style="color:#666">.</span><span style="color:#b44">retrieve</span><span style="color:#666">(</span>AccountManagement<span style="color:#666">.</span><span style="color:#b44">class</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#a2f;font-weight:bold">if</span><span style="color:#666">(</span><span style="color:#666">!</span>accountManagement<span style="color:#666">.</span><span style="color:#b44">verifyAccountExists</span><span style="color:#666">(</span>EntityImpl<span style="color:#666">.</span><span style="color:#b44">parse</span><span style="color:#666">(</span><span style="color:#b44">&#34;user1@myembeddedjabber.com&#34;</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">)</span> <span style="color:#666">{</span>
accountManagement<span style="color:#666">.</span><span style="color:#b44">addUser</span><span style="color:#666">(</span>EntityImpl<span style="color:#666">.</span><span style="color:#b44">parse</span><span style="color:#666">(</span><span style="color:#b44">&#34;user1@myembeddedjabber.com&#34;</span><span style="color:#666">)</span><span style="color:#666">,</span> <span style="color:#b44">&#34;password1&#34;</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
</code></pre></div><p>Now, instantiate the server and set the fundamental stuff: endpoint, user management and TLS.</p>
<p>SSL needs a TLS certificate. There is one coming along with Vysper for testing purposes only, just make sure it is properly referenced on the file system.
Or, even better, create your own.</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">XMPPServer server <span style="color:#666">=</span> <span style="color:#a2f;font-weight:bold">new</span> XMPPServer<span style="color:#666">(</span><span style="color:#b44">&#34;myembeddedjabber.com&#34;</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">addEndpoint</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> TCPEndpoint<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">setStorageProviderRegistry</span><span style="color:#666">(</span>providerRegistry<span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">setTLSCertificateInfo</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> File<span style="color:#666">(</span><span style="color:#b44">&#34;src/main/config/bogus_mina_tls.cert&#34;</span><span style="color:#666">)</span><span style="color:#666">,</span> <span style="color:#b44">&#34;boguspw&#34;</span><span style="color:#666">)</span><span style="color:#666">;</span>
</code></pre></div><p>With initializing completed, just give it a go:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#a2f;font-weight:bold">try</span> <span style="color:#666">{</span>
server<span style="color:#666">.</span><span style="color:#b44">start</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
System<span style="color:#666">.</span><span style="color:#b44">out</span><span style="color:#666">.</span><span style="color:#b44">println</span><span style="color:#666">(</span><span style="color:#b44">&#34;server is running...&#34;</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span> <span style="color:#a2f;font-weight:bold">catch</span> <span style="color:#666">(</span>Exception e<span style="color:#666">)</span> <span style="color:#666">{</span>
e<span style="color:#666">.</span><span style="color:#b44">printStackTrace</span><span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">;</span>
<span style="color:#666">}</span>
</code></pre></div><h2 id="adding-modules-to-the-server">Adding modules to the server.</h2>
<p>The server starts up with two built-in modules, Roster and Service Discovery. Both can be considered as optional, but for a proper server you would normally want to have them (and if you don&rsquo;t, remove them from code in XMPPServer.start() and rebuild).</p>
<p>Some handy modules can be added, but this is optional:</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">server<span style="color:#666">.</span><span style="color:#b44">addModule</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> SoftwareVersionModule<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">addModule</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> EntityTimeModule<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span>
server<span style="color:#666">.</span><span style="color:#b44">addModule</span><span style="color:#666">(</span><span style="color:#a2f;font-weight:bold">new</span> VcardTempModule<span style="color:#666">(</span><span style="color:#666">)</span><span style="color:#666">)</span><span style="color:#666">;</span> <span style="color:#080;font-style:italic">// depends on Jcr persistence
</span></code></pre></div><h2 id="reference">Reference</h2>
<p>The full and probably improved latest version of this code can be found <a href="https://gitbox.apache.org/repos/asf?p=mina-vysper.git;a=blob;f=server/core/src/main/java/org/apache/vysper/xmpp/server/ServerMain.java;h=379d96b06d526609c6c73927ce95d821e9746635;hb=HEAD">here</a></p>
</div>
<div id="endContent"></div>
</div>
<div id="footer">
&copy; 2003-2024, <a href="https://www.apache.org">The Apache Software Foundation</a> - <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy Policy</a><br />
Apache MINA, MINA, Apache Vysper, Vysper, Apache SSHd, SSHd, Apache FtpServer, FtpServer, Apache AsyncWeb, AsyncWeb,
Apache, the Apache feather logo, and the Apache Mina project logos are trademarks of The Apache Software Foundation.
</div>
</div>
</body>
</html>