2016-01-14 19:38:06 -08:00

166 lines
9.8 KiB
HTML

<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Plugin Services / IntelliJ Platform SDK DevGuide</title>
<link rel="stylesheet" href="/intellij/sdk/docs/app/css/styles.min.css">
<!-- non-retina iPad pre iOS 7 -->
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-72x72.png" sizes="72x72">
<!-- retina iPhone pre iOS 7 -->
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-114x114.png" sizes="114x114">
<!-- retina iPad pre iOS 7 -->
<link rel="apple-touch-icon" href="/intellij/sdk/docs/apple-touch-icon-144x144.png" sizes="144x144">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/intellij/sdk/docs/apple-touch-icon-precomposed.png">
<!-- normal favicon -->
<link rel="shortcut icon" type="image/x-icon" href="/intellij/sdk/docs/favicon.ico">
<link rel="icon" type="image/png" href="/intellij/sdk/docs/favicon.png">
<link rel="stylesheet" href="/intellij/sdk/docs/styles/styles.css"></head>
<body data-id="basics/plugin_structure/plugin_services">
<div class="wrapper">
<section class="panel _nav">
<header class="panel__header">
<div class="container">
<form class="search-box">
<label for="search-box__input" class="search-box__label">
<input type="text" class="search-box__input" id="search-box__input" placeholder="Search IntelliJ Platform SDK DevGuide">
</label>
<div class="search-box__clear" title="Clear"></div>
</form>
</div>
</header>
<nav class="panel__content">
<div class="container _nav">
<menu class="nav-tree"></menu>
</div>
<div class="container _footer panel__footer">
<p><a href="https://youtrack.jetbrains.com/issues/IJSDK">Send feedback</a></p>
<p>&copy; 2000&ndash;2016 <a href="//www.jetbrains.com">JetBrains</a> s.r.o.<br>
All rights reserved.</p>
</div>
</nav>
</section>
<main class="panel _main" role="main">
<header class="panel__header">
<div class="container">
<h3>IntelliJ Platform SDK DevGuide</h3>
<div class="shortcuts-switcher"><label for="switch-shortcuts">Keymap:</label><select id="switch-shortcuts" class="select _shortcuts" height="1">
<option data-group="primary" value="default" selected>Default</option>
<option data-group="primary" value="default_for_gnome">GNOME</option>
<option data-group="primary" value="default_for_kde">KDE</option>
<option data-group="primary" value="default_for_xwin">XWindow</option>
<option data-group="primary" value="emacs">Emacs</option>
<option data-group="primary" value="jbuilder">JBuilder</option>
<option data-group="primary" value="visual_studio">Visual Studio</option>
<option data-group="primary" value="netbeans_6.5">NetBeans 6.5</option>
<option data-group="primary" value="eclipse">Eclipse</option>
<option data-group="secondary" value="mac_os_x_10.5_">OS X 10.5+</option>
<option data-group="secondary" value="mac_os_x">OS X</option>
<option data-group="secondary" value="eclipse_mac_os_x">OS X Eclipse</option></select>
</div>
<div class="panel-trigger"></div>
</div>
</header>
<section class="panel__content">
<div class="container">
<article class="article" data-shortcut-switcher="false">
<h1>Plugin Services</h1>
<p>The <em>IntelliJ Platform</em> provides the concept of <em>services</em>.</p>
<p>A <em>service</em> is a plugin component loaded on demand, when your plugin calls the <code class="code highlight language-text">getService</code> method of the <a href="https://upsource.jetbrains.com/idea-ce/file/idea-ce-1731d054af4ca27aa827c03929e27eeb0e6a8366/platform/core-api/src/com/intellij/openapi/components/ServiceManager.java" data-bypass="yes" target="_blank"><span>ServiceManager</span></a> class.</p>
<p>The <em>IntelliJ Platform</em> ensures that only one instance of a service is loaded even though the service is called several times. A service must have the interface and implementation classes specified in the <code class="code highlight language-text">plugin.xml</code> file. The service implementation class is used for service instantiation.</p>
<p>The <em>IntelliJ Platform</em> offers three types of services: <em>application level</em> services, <em>project level</em> services and <em>module level</em> services.</p>
<a name="how-to-declare-a-service" class="elem-anchor"></a>
<h2>How to Declare a Service?<a href="#how-to-declare-a-service" class="anchor-link"><span></span></a></h2>
<p>To declare a service, you can use the following extension points in the IDEA core:</p>
<ul>
<li><code class="code highlight language-text">applicationService</code>: designed to declare an application level service.</li>
<li><code class="code highlight language-text">projectService</code>: designed to declare a project level service.</li>
<li><code class="code highlight language-text">moduleService</code>: designed to declare a module level service.</li>
</ul>
<p><strong>To declare a service:</strong></p>
<ol>
<li>Add the appropriate child element (<code class="code highlight language-text">&lt;applicationService&gt;</code>, <code class="code highlight language-text">&lt;projectService&gt;</code> or <code class="code highlight language-text">&lt;moduleService&gt;</code>) to the <code class="code highlight language-text">&lt;extensions&gt;</code> section of the plugin.xml file.</li>
<li>For the newly added child element, set the following attributes:
<ul>
<li><code class="code highlight language-text">serviceInterface</code>: specifies the service interface class.</li>
<li><code class="code highlight language-text">serviceImplementation</code>: specifies the service implementation class.</li>
</ul>
</li>
</ol>
<p>Note that the interface and implementation classes can be the same.</p>
<p>To clarify the service declaration procedure, consider the following fragment of the <code class="code highlight language-text">plugin.xml</code> file:</p>
<pre><code class="code-block__wrapper code-block _highlighted lang_xml"><span class="nt">&lt;extensions</span> <span class="na">defaultExtensionNs=</span><span class="s">"com.intellij"</span><span class="nt">&gt;</span>
<span class="c">&lt;!-- Declare the application level service --&gt;</span>
<span class="nt">&lt;applicationService</span> <span class="na">serviceInterface=</span><span class="s">"Mypackage.MyServiceInterfaceClass"</span> <span class="na">serviceImplementation=</span><span class="s">"Mypackage.MyServiceImplClass"</span> <span class="nt">/&gt;</span>
<span class="c">&lt;!-- Declare the project level service --&gt;</span>
<span class="nt">&lt;projectService</span> <span class="na">serviceInterface=</span><span class="s">"Mypackage.MyProjectServiceInterfaceClass"</span> <span class="na">serviceImplementation=</span><span class="s">"Mypackage.MyProjectServiceImplClass"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/extensions&gt;</span>
</code></pre>
<a name="retrieving-a-service" class="elem-anchor"></a>
<h2>Retrieving a service<a href="#retrieving-a-service" class="anchor-link"><span></span></a></h2>
<p>To instantiate your service, in Java code, use the following syntax:</p>
<pre><code class="code-block__wrapper code-block _highlighted lang_java"><span class="n">MyServiceImplClass</span> <span class="n">service</span> <span class="o">=</span> <span class="n">ServiceManager</span><span class="o">.</span><span class="na">getService</span><span class="o">(</span><span class="n">MyServiceImplClass</span><span class="o">.</span><span class="na">class</span><span class="o">);</span>
</code></pre>
<a name="sample-plugin" class="elem-anchor"></a>
<h3>Sample Plugin<a href="#sample-plugin" class="anchor-link"><span></span></a></h3>
<p>This section allows you to download and install a sample plugin illustrating how to create and use a plugin service. This plugin has a project component implementing a service that counts the number of currently opened projects in the IDE. If this number exceeds the maximum allowed number of simultaneously opened projects, the plugin returns an error message and closes the most recently opened project.</p>
<!-- TODO Replace with other plugin URL when available-->
<p><strong>To install and run the sample plugin</strong></p>
<ul>
<li>Download the included sample plugin project located <a href="https://github.com/JetBrains/intellij-sdk-docs/tree/master/code_samples/MaxOpenedProjects" data-bypass="yes" target="_blank"><span>here</span></a>.</li>
<li>Start <em>IntelliJ IDEA</em>, on the starting page, click <em>Open Project</em>, and then use the <em>Open Project</em> dialog box to open the project <em>MaxOpenedProjects</em>.</li>
<li>On the main menu, choose <em>Run | Run</em> or press <kbd>Shift</kbd>+<kbd>F10</kbd>.</li>
<li>If necessary, change the <a href="http://www.jetbrains.com/idea/help/run-debug-configuration-plugin.html" data-bypass="yes" target="_blank"><span>Run/Debug Configurations</span></a>.</li>
</ul>
<div class="last-modified">
Last modified: 3 December 2015
</div>
</article>
<section class="disqus">
<div id="disqus_thread"></div>
</section>
</div>
</section>
</main>
</div>
<script data-main="/intellij/sdk/docs/app/js/main.build" data-baseurl="/intellij/sdk/docs/" src="/intellij/sdk/docs/app/js/vendor/requirejs/require.js"></script>
</body>
</html>