<?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>OSGI Archives - Sourced Code</title>
	<atom:link href="https://sourcedcode.com/blog/category/aem/osgi/feed" rel="self" type="application/rss+xml" />
	<link>https://sourcedcode.com</link>
	<description>AEM Blog Made Just for You.</description>
	<lastBuildDate>Fri, 24 Jun 2022 02:55:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.8</generator>

<image>
	<url>https://sourcedcode.com/wp-content/uploads/2019/09/cropped-favicon-32x32.png</url>
	<title>OSGI Archives - Sourced Code</title>
	<link>https://sourcedcode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Send Out Emails with AEM Servlet</title>
		<link>https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet</link>
					<comments>https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Fri, 21 Jan 2022 03:31:31 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[OSGI]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3339</guid>

					<description><![CDATA[<p>In this article, we will be setting up the local AEM author developer&#8217;s environment to send out an email utilizing the Day CQ Mail Service. This article includes code snippets to a Sling Servlet that will be used to send out the email. We will be using a Google Gmail account as the SMTP Mail [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet">Send Out Emails with AEM Servlet</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this article, we will be setting up the local AEM author developer&#8217;s environment to send out an email utilizing the Day CQ Mail Service. This article includes code snippets to a Sling Servlet that will be used to send out the email.</p>
<div class="mention-block">
We will be using a Google Gmail account as the SMTP Mail Server in this article. To learn how to set up your Google Gmail account as the SMTP server, <a href="https://sourcedcode.com/blog/general-web/how-to-use-the-gmail-smtp-server-to-send-emails-for-free" rel="noopener" target="_blank">click here</a>.
</div>
<hr class="spacer-large" />
<h2>1. Configure the Day CQ Mail Service</h2>
<p>a. Visit <a href="http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService" rel="noopener" target="_blank">http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService</a></p>
<p><a class="foobox fbx-instance fbx-link" href="https://sourcedcode.com/wp-content/uploads/2022/01/step-1-send-email-aem.png"><img decoding="async" width="300" src="https://sourcedcode.com/wp-content/uploads/2022/01/step-1-send-email-aem.png" alt="This is a screenshot of the Apache Felix Console, Day CQ Mail Service Configuration"/></a></p>
<p>b. Include the configurations below:<br />
<strong>Google SMTP Configurations</strong><br />
SMTP server hostname: smtp.gmail.com<br />
SMTP server port: 465, 25 or 587 (test in that order)<br />
SMTP user: @@YOUR_EMAIL_ADDRESS@@<br />
SMTP from emai: @@YOUR_EMAIL_ADDRESS@@<br />
SMTP password: @@YOUR_PASSWORD@@<br />
SMTP TLS/SSL: true</p>
<p>To automate this configuration, you can run a Curl command like the example below. But please be aware, you must use your own Google Gmail account. </p>
<p>To update the Curl command below, use this tool here, <a class="code-highlight-primary" href="https://sourcedcode.com/blog/aem/tool-generate-curl-command-to-update-osgi-configurations?aemUrl=http://localhost:4502&#038;username=admin&#038;password=admin&#038;pid=com.day.cq.mailer.DefaultMailService&#038;osgiKeys=smtp.host,smtp.port,smtp.user,smtp.password,from.address,smtp.ssl&#038;osgiValues=smtp.gmail.com,587,contact@sourcedcode.com,p4ssw0rd,contact@sourcedcode.com,true#curltool" target="_blank" rel="noopener">Tool: Generate Curl Command to Update OSGI Configurations</a></p>
<pre style="white-space: pre-wrap; font-size: 12px; padding: 5px;">
curl -u admin:admin 'http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService' --data-raw 'apply=true&action=ajaxConfigManager&%24location=&smtp.host=smtp.gmail.com&smtp.port=587&smtp.user=contact@sourcedcode.com&smtp.password=p4ssw0rd&from.address=contact@sourcedcode.com&smtp.ssl=true&propertylist=smtp.host%2Csmtp.port%2Csmtp.user%2Csmtp.password%2Cfrom.address%2Csmtp.ssl'
</pre >
<hr class="spacer-large" />
<h2>2. Create the SendEmailServletExample AEM Servlet</h2>
<p>Create a Java class named, SendEmailServletExample.java, copy and paste the code below, replace line:44 with your own email, and then use your Java Maven skills to build the servlet in AEM.</p>
<p>Once when the SendEmailServletExample.java is built into AEM, visit <a class="code-highlight-primary" href="http://localhost:4502/bin/sendemail" rel="noopener" target="_blank">http://localhost:4502/bin/sendemail</a>,</p>
<div class="code-1000">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.servlet</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.mailer.MessageGateway</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.mailer.MessageGatewayService</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.Email</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.EmailException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.HtmlEmail</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Reference</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.Logger</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.LoggerFactory</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">sling</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">servlets</span>.<span style="color: #006633;">ServletResolverConstants</span>.<span style="color: #006633;">SLING_SERVLET_PATHS</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SLING_SERVLET_PATHS <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=/bin/sendemail&quot;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SendEmailServletExample <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>SendEmailServletExample.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> MessageGatewayService messageGatewayService<span style="color: #339933;">;</span><br />
<br />
&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; JSONObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> sent <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> recipients <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;contact@sourcedcode.com&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; sendEmail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is an test email&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;This is the email body&quot;</span>, recipients<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; sent <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>EmailException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result&quot;</span>, sent <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;done&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;something went wrong&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JSONException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> sendEmail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> subjectLine, <span style="color: #003399;">String</span> msgBody, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> recipients<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> EmailException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Email email <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HtmlEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> recipient <span style="color: #339933;">:</span> recipients<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; email.<span style="color: #006633;">addTo</span><span style="color: #009900;">&#40;</span>recipient, recipient<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; email.<span style="color: #006633;">setSubject</span><span style="color: #009900;">&#40;</span>subjectLine<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; email.<span style="color: #006633;">setMsg</span><span style="color: #009900;">&#40;</span>msgBody<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; MessageGateway<span style="color: #339933;">&lt;</span>Email<span style="color: #339933;">&gt;</span> messageGateway <span style="color: #339933;">=</span> messageGatewayService.<span style="color: #006633;">getGateway</span><span style="color: #009900;">&#40;</span>HtmlEmail.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>messageGateway <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sending out email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; messageGateway.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Email<span style="color: #009900;">&#41;</span> email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The message gateway could not be retrieved.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet">Send Out Emails with AEM Servlet</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Get Cookie Example of AEM Servlet and Sling Model</title>
		<link>https://sourcedcode.com/blog/aem/get-cookie-example-of-aem-servlet-and-sling-model</link>
					<comments>https://sourcedcode.com/blog/aem/get-cookie-example-of-aem-servlet-and-sling-model#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 19 Jan 2022 04:58:31 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[OSGI]]></category>
		<category><![CDATA[Short Post]]></category>
		<category><![CDATA[Sling Models]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3277</guid>

					<description><![CDATA[<p>In this short article, we will show two code examples; for example, one will be showcasing how cookies values can be accessed for an AEM Servlet, and for example two, Sling Model. 1. AEM Servlet Get Cookie Example 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465package com.sourcedcode.core.servlet; import com.cat.wcm.core.util.JSONObject; import org.apache.commons.lang.StringUtils; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.osgi.service.component.annotations.Component; import javax.servlet.Servlet; import [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/get-cookie-example-of-aem-servlet-and-sling-model">Get Cookie Example of AEM Servlet and Sling Model</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this short article, we will show two code examples; for example, one will be showcasing how cookies values can be accessed for an AEM Servlet, and for example two, Sling Model.</p>
<hr class="spacer-large" />
<h2>1. AEM Servlet Get Cookie Example</h2>
<div class="code-1000">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.servlet</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.cat.wcm.core.util.JSONObject</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.StringUtils</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.Cookie</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">sling</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">servlets</span>.<span style="color: #006633;">ServletResolverConstants</span>.<span style="color: #006633;">SLING_SERVLET_PATHS</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SLING_SERVLET_PATHS <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=/bin/getCookieExample&quot;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GetCookieServletExample <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Cookie myCookie <span style="color: #339933;">=</span> getCookie<span style="color: #009900;">&#40;</span>request, <span style="color: #0000ff;">&quot;myCookie&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> myCookieValue <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>myCookie <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>myCookie.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isBlank</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><br />
&nbsp; &nbsp; &nbsp; myCookieValue <span style="color: #339933;">=</span> myCookie.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// server response</span><br />
&nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; JSONObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myCookie&quot;</span>, myCookieValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; <span style="color: #666666; font-style: italic;">// extracted helper method</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> Cookie getCookie<span style="color: #009900;">&#40;</span>HttpServletRequest request, <span style="color: #003399;">String</span> cookieName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">isBlank</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Cookie<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> cookies <span style="color: #339933;">=</span> request.<span style="color: #006633;">getCookies</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cookies <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cookies.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cookie<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> var3 <span style="color: #339933;">=</span> cookies<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> var4 <span style="color: #339933;">=</span> cookies.<span style="color: #006633;">length</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> var5 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> var5 <span style="color: #339933;">&lt;</span> var4<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>var5<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cookie cookie <span style="color: #339933;">=</span> var3<span style="color: #009900;">&#91;</span>var5<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>cookieName, cookie.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> cookie<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<hr class="spacer-large" />
<h2>2. Sling Model Get Cookie Example</h2>
<div class="code-750">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.models.pages.page</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.StringUtils</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.DefaultInjectionStrategy</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Model</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.SlingObject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.PostConstruct</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.Cookie</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span><br />
<br />
@Model<span style="color: #009900;">&#40;</span><br />
&nbsp; adaptables <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>SlingHttpServletRequest.<span style="color: #000000; font-weight: bold;">class</span>, Resource.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; defaultInjectionStrategy <span style="color: #339933;">=</span> DefaultInjectionStrategy.<span style="color: #006633;">OPTIONAL</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExampleModel <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; @SlingObject<br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> SlingHttpServletRequest request<span style="color: #339933;">;</span><br />
<br />
&nbsp; @PostConstruct<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Cookie myCookie <span style="color: #339933;">=</span> getCookie<span style="color: #009900;">&#40;</span>request, <span style="color: #0000ff;">&quot;myCookie&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> myCookieValue <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>myCookie <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>myCookie.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isBlank</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><br />
&nbsp; &nbsp; &nbsp; myCookieValue <span style="color: #339933;">=</span> myCookie.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// extracted helper method</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> Cookie getCookie<span style="color: #009900;">&#40;</span>HttpServletRequest request, <span style="color: #003399;">String</span> cookieName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">isBlank</span><span style="color: #009900;">&#40;</span>cookieName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Cookie<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> cookies <span style="color: #339933;">=</span> request.<span style="color: #006633;">getCookies</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cookies <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cookies.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cookie<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> var3 <span style="color: #339933;">=</span> cookies<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> var4 <span style="color: #339933;">=</span> cookies.<span style="color: #006633;">length</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> var5 <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> var5 <span style="color: #339933;">&lt;</span> var4<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>var5<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cookie cookie <span style="color: #339933;">=</span> var3<span style="color: #009900;">&#91;</span>var5<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>StringUtils.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>cookieName, cookie.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> cookie<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/get-cookie-example-of-aem-servlet-and-sling-model">Get Cookie Example of AEM Servlet and Sling Model</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/get-cookie-example-of-aem-servlet-and-sling-model/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>AEM inheritedpageproperties with Sightly, JSP, OSGI Bundle</title>
		<link>https://sourcedcode.com/blog/aem/aem-quick-reference/aem-inheritedpageproperties-with-sightly-jsp-osgi-bundle</link>
					<comments>https://sourcedcode.com/blog/aem/aem-quick-reference/aem-inheritedpageproperties-with-sightly-jsp-osgi-bundle#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sun, 20 Sep 2020 17:18:31 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[OSGI]]></category>
		<category><![CDATA[Quick Reference]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=2232</guid>

					<description><![CDATA[<p>inheritedpageproperties helps you retrieve a parent property from parent pages. Inherited page properties utilises com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap, where the search is made on the page hierarchy, upwards; searching children of the page nodes (components) are not included. Inherited page properties can be retrieved in multiple ways, where this article will only focus on 3 ways. In this [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-quick-reference/aem-inheritedpageproperties-with-sightly-jsp-osgi-bundle">AEM inheritedpageproperties with Sightly, JSP, OSGI Bundle</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>inheritedpageproperties helps you retrieve a parent property from parent pages. Inherited page properties utilises <a href="https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/commons/inherit/HierarchyNodeInheritanceValueMap.html" title="https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/com/day/cq/commons/inherit/HierarchyNodeInheritanceValueMap.html" rel="noopener nofollow noreferrer" target="_blank">com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap</a>, where the search is made on the page hierarchy, upwards; searching children of the page nodes (components) are not included. Inherited page properties can be retrieved in multiple ways, where this article will only focus on 3 ways.</p>
<p>In this article, we will go through the 3 different ways in how we can retrieve inherited page properties.</p>
<ol>
<li><a href="#inheritedPageProperties-how-it-works">How does this work?</a></li>
<li><a href="#inheritedPageProperties-with-sightly">inheritedPageProperties with Sightly</a></li>
<li><a href="#inheritedPageProperties-with-jsp">inheritedPageProperties with JSP</a></li>
<li><a href="#inheritedPageProperties-with-java-backend">inheritedPageProperties with OSGI Bundle, Java Backend</a></li>
</ol>
<hr class="spacer-large"/>
<h2 id="inheritedPageProperties-how-it-works">How does this work?</h2>
<p>An InheritanceValueMap for a given Resource that will inherit values from ancestral pages.<br />
For example, given: /content/parent/page/jcr:content/footer/image/@width, the HierarchyNodeInheritanceValueMap will search for a footer/image/@width property in:</p>
<p><strong>look up one:</strong> /content/parent/<strong>currentPage</strong>/jcr:content/footer/image/@width<br />
<strong>look up two:</strong> /content/parent/jcr:content/footer/image/@width<br />
<strong>look up three:</strong> /content/jcr:content/footer/image/@width</p>
<hr class="spacer-large"/>
<h2 id="inheritedPageProperties-with-sightly">1. inheritedPageProperties with Sightly</h2>
<p>Sightly offers a plethora of <a href="/blog/aem/aem-global-objects-for-backend-and-front-end-sightly-htl" rel="noopener noreferrer" target="_blank">AEM Global objects</a>, and one of them notably is the &#8220;inheritedPageProperties&#8221;. Utilising the inheritedPageProperties global object, we can use dot or square bracket notation to access the interited page property value.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">${inheritedPageProperties.myCustomProperty}<br />
${inheritedPageProperties['myCustomProperty']}<br />
${inheritedPageProperties.jcr:title}<br />
${inheritedPageProperties['jcr:title']}</div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="4202452438"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<hr class="spacer-large"/>
<h2 id="inheritedPageProperties-with-jsp">2. inheritedPageProperties with JSP</h2>
<p>Utilising the JSP include, /libs/foundation/global.jsp (purpose of retreiving the currentPage AEM global object), we can make use of the HierarchyNodeInheritanceValueMap.class, and currentPage resource to initialise the InheritanceValueMap.class object. Next, calling the method <a href="https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/com/day/cq/commons/inherit/HierarchyNodeInheritanceValueMap.html" rel="noopener noreferrer" target="_blank">getInherited(String name, Class&lt;T&gt; type)</a>, we are able to get the inherited page property.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;%@include file<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/libs/foundation/global.jsp&quot;</span>%&gt;<br />
&lt;%@ page import<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;com.day.cq.commons.inherit.InheritanceValueMap&quot;</span> %&gt;<br />
&lt;%@ page import<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap&quot;</span> %&gt;<br />
<br />
&lt;%<br />
&nbsp; &nbsp; InheritanceValueMap ivm <span style="color: #66cc66;">=</span> new HierarchyNodeInheritanceValueMap<span style="color: #66cc66;">&#40;</span>currentPage.getContentResource<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; String inheritedValueMyCustomProperty <span style="color: #66cc66;">=</span> ivm.getInherited<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myCustomerProperty&quot;</span>, String.<span style="color: #000066;">class</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; String inheritedValueJcrTitle <span style="color: #66cc66;">=</span> ivm.getInherited<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;jcr:title&quot;</span>, String.<span style="color: #000066;">class</span><span style="color: #66cc66;">&#41;</span>;<br />
%&gt;<br />
inheritedValueMyCustomProperty <span style="color: #66cc66;">=</span> &lt;%<span style="color: #66cc66;">=</span> inheritedValueMyCustomProperty %&gt;<br />
inheritedValueJcrTitle <span style="color: #66cc66;">=</span> &lt;%<span style="color: #66cc66;">=</span> inheritedValueJcrTitle %&gt;</div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<h2 id="inheritedPageProperties-with-java-backend">3. inheritedPageProperties with OSGI Bundle, Java Backend</h2>
<p>Similar to the JSP implementation above, we can make use of the HierarchyNodeInheritanceValueMap.class, and currentPage resource to initialise the InheritanceValueMap.class object. Next, calling the method <a href="https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/com/day/cq/commons/inherit/HierarchyNodeInheritanceValueMap.html" rel="noopener noreferrer" target="_blank">getInherited(String name, Class&lt;T&gt; type)</a>, we are able to get the inherited page property.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.commons.inherit.InheritanceValueMap</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap</span><span style="color: #339933;">;</span><br />
<br />
@ScriptVariable <br />
<span style="color: #000000; font-weight: bold;">private</span> Page currentPage<span style="color: #339933;">;</span><br />
...<br />
<span style="color: #006633;">InheritanceValueMap</span> ivm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HierarchyNodeInheritanceValueMap<span style="color: #009900;">&#40;</span>currentPage.<span style="color: #006633;">getContentResource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003399;">String</span> inheritedValueMyCustomProperty <span style="color: #339933;">=</span> ivm.<span style="color: #006633;">getInherited</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myCustomerProperty&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003399;">String</span> inheritedValueJcrTitle <span style="color: #339933;">=</span> ivm.<span style="color: #006633;">getInherited</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:title&quot;</span>, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="8658919803"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-quick-reference/aem-inheritedpageproperties-with-sightly-jsp-osgi-bundle">AEM inheritedpageproperties with Sightly, JSP, OSGI Bundle</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-quick-reference/aem-inheritedpageproperties-with-sightly-jsp-osgi-bundle/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>How to Enable Configuration for OSGI Components with R6</title>
		<link>https://sourcedcode.com/blog/aem/how-to-enable-configuration-for-osgi-components-with-r6</link>
					<comments>https://sourcedcode.com/blog/aem/how-to-enable-configuration-for-osgi-components-with-r6#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 06 Jun 2020 20:34:05 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[OSGI]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=1738</guid>

					<description><![CDATA[<p>Adding configuration to OSGi Component(s) is as simple as annotating your class with @Designate(ocd=&#8221;&#8221;) annotation with the &#8220;ocd&#8221; property, and to make the @Activate method accepts an config param; as indicated below: 1234567891011121314// /com/sourcedcode/services/impl/MyserviceImpl.java import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.metatype.annotations.Designate; @Component&#40;service = MyService.class, immediate = true&#41; @Designate&#40;ocd = SourcedCodeSiteSettingsConfig.class&#41; public class MyserviceImpl extends MyService &#123; [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/how-to-enable-configuration-for-osgi-components-with-r6">How to Enable Configuration for OSGI Components with R6</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Adding configuration to OSGi Component(s) is as simple as annotating your class with <span class="code-highlight-primary">@Designate(ocd=&#8221;&#8221;)</span> annotation with the &#8220;ocd&#8221; property, and to make the @Activate method accepts an config param; as indicated below:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// /com/sourcedcode/services/impl/MyserviceImpl.java</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Activate</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.Designate</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> MyService.<span style="color: #000000; font-weight: bold;">class</span>, immediate <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
@Designate<span style="color: #009900;">&#40;</span>ocd <span style="color: #339933;">=</span> SourcedCodeSiteSettingsConfig.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyserviceImpl <span style="color: #000000; font-weight: bold;">extends</span> MyService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; @Activate<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> activate<span style="color: #009900;">&#40;</span>SourcedCodeSiteSettingsConfig config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// do something</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// String name = config.name();</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The &#8220;ocd&#8221; property must be set with a configuration interface class that is annotated with @ObjectClassDefinition; the annotated interface class can be an <a href="#imported-class">imported Java interface class</a> or <a href="#inner-class">inner Java interface class</a>. </p>
<p>Once the @Designate annotation on the OSGI Component&#8217;s class and the @ObjectClassDefinition configuration interface are configured, the OSGI Component(s) will have <strong>enabled the configuration successfully</strong>; you should be able to find the configuration in the OSGI Apache Felix Console, under <a href="http://localhost:4502/system/console/configMgr" target="_blank" rel="noopener noreferrer">http://localhost:4502/system/console/configMgr</a>.</p>
<div class="mention-block">
<strong>Quick Links with Code Samples:</strong><br />
This article will highlight two different ways to enable configuration to your OSGI Component(s).</p>
<ol>
<li><a href="#imported-class">Imported Class &#8211; Enable OSGI Component&#8217;s Configuration Implementation</a></li>
<li><a href="#inner-class">Inner Class &#8211; Enable OSGI Component&#8217;s Configuration Implementation</a></li>
</ol>
</div>
<hr class="spacer-large" />
<h4>What is the @ObjectClassDefinition annotation?</h4>
<p>The configuration interface is annotated with <span class="code-highlight-primary">@ObjectClassDefinition</span> where properties can be passed into the @ObjectClassDefinition annotation. The most common properties are @ObjectClassDefinition(name=&#8221;&#8221;, description=&#8221;&#8221;). The &#8220;name&#8221; property will be used as the title of the configuration <a class="foobox" title="OSGI Apache Felix Console - Active Configuration Dialog" href="https://sourcedcode.com/wp-content/uploads/2020/06/blog-img-how-to-enable-configuration-to-osgi-components-with-r6.png" target="_blank" rel="noopener noreferrer">popup</a> within the OSGI Felix console, while the description property is used for the &#8220;description&#8221; of the <a class="foobox" href="https://sourcedcode.com/wp-content/uploads/2020/06/blog-img-how-to-enable-configuration-to-osgi-components-with-r6.png" target="_blank" rel="noopener noreferrer" title="OSGI Apache Felix Console - Active Configuration Dialog">popup</a>.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #666666; font-style: italic;">// Example:</span><br />
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.services</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.ObjectClassDefinition</span><span style="color: #339933;">;</span><br />
<br />
@ObjectClassDefinition<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SourcedCode - Site Settings Configuration&quot;</span>,<br />
&nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;dialog description&quot;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> SourcedCodeSiteSettingsConfigExample <span style="color: #009900;">&#123;</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// config1 @AttributeDefinition()</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// config2 @AttributeDefinition()</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// config3 @AttributeDefinition()</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4>What is the @AttributeDefinition annotation?</h4>
<p>Declaration of the <span class="code-highlight-primary">@AttributeDefinition</span> annotations defines each configuration by identifying the type, the configuration reference name, config name and description.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.services</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.AttributeDefinition</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.AttributeType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.ObjectClassDefinition</span><span style="color: #339933;">;</span><br />
<br />
@ObjectClassDefinition<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SourcedCode - Site Settings Configuration&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;dialog description&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> SourcedCodeSiteSettingsConfigExample <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String[] Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String[] Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> config_string_array_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;item1&quot;</span>, <span style="color: #0000ff;">&quot;item2&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// config2 @AttributeDefinition()</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// config3 @AttributeDefinition()</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="mention-block"><strong>Note!</strong><br />
To wrap your head around some of the essentials of @AttributeDefinition, checkout this blog article : <a href="https://sourcedcode.com/blog/aem/osgi-r6-configuration-attributedefinition-essentials-reference-guide" target="_blank" rel="noopener noreferrer">OSGI R6 Configuration @AttributeDefinition Essentials Reference Guide</a>.</div>
<hr class="spacer-large" />
<h2 id="imported-class">1. Imported Class &#8211; Defining OSGI Component&#8217;s Configuration with Configuration Interface</h2>
<p>The title of this section speaks for itself. The examples below <a href="#"imported-class-step-a">&quot;Step A&quot;</a> example provides sample code of a configuration interface class that is prepared to be imported OSGI Service Component, indicated in <a href="#"imported-class-step-b">&quot;Step B&quot;</a>. This class have one responsibility, and its to provide the definition of the OSGI Component&#8217;s configuration interface.</p>
<div class="spacer-large"></div>
<h3 id="imported-class-step-a">Step A. Defining the annotated Configuration Interface</h3>
<p>Isolated configuration interface class that prepared to be imported and referenced by the OSGI Service Component.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.services</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.AttributeDefinition</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.AttributeType</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.ObjectClassDefinition</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.Option</span><span style="color: #339933;">;</span><br />
<br />
@ObjectClassDefinition<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SourcedCode - Site Settings Configuration&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;dialog description&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> @<span style="color: #000000; font-weight: bold;">interface</span> SourcedCodeSiteSettingsConfig <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> config_string_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;Default String&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String[] Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String[] Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> config_string_array_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;item1&quot;</span>, <span style="color: #0000ff;">&quot;item2&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Long Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Long Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">LONG</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> config_long_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> 0L<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;int Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;innt Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">INTEGER</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> config_number_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Short Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Short Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">SHORT</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">short</span> config_short_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Char Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Char Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">CHARACTER</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">char</span> config_char_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Byte Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Byte Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">BYTE</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">byte</span> config_byte_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Double Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Double Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">DOUBLE</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">double</span> config_double_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Float Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Float Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">FLOAT</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">float</span> config_float_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Boolean Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Boolean Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">BOOLEAN</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> config_boolean_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Password Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Password Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">PASSWORD</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> config_password_config_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Dropdown Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Dropdown Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PRODUCTION&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PRODUCTION&quot;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;STAGING&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;STAGING&quot;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UAT&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UAT&quot;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;QA&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;QA&quot;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DEVELOP&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DEVELOP&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #003399;">String</span> config_dropdown_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;DEVELOP&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="spacer-large"></div>
<h3 id="imported-class-step-b">Step B. Consuming the Configuration Interface by Imported Class</h3>
<p>To use the configuration interface within our OSGI service component, we would need to annotation our service class with <span class="code-highlight-primary">@Designate</span>, with the ocd param pointing to the imported configuration interface class. Next, it would be as simple as setting up the initial @Activate method.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.services</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Activate</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.Designate</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourcedcode.core.services.SourcedCodeSiteSettingsConfig</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> SourcedCodeSiteSettingsService.<span style="color: #000000; font-weight: bold;">class</span>, immediate <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
@Designate<span style="color: #009900;">&#40;</span>ocd <span style="color: #339933;">=</span> SourcedCodeSiteSettingsConfig.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SourcedCodeSiteSettingsService <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> config_string_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> config_string_array_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> config_long_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> config_number_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">short</span> config_short_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">char</span> config_char_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">byte</span> config_byte_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> config_double_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> config_float_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Boolean</span> config_boolean_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> config_password_getConfig_example<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> config_dropdown_example<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Activate<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> activate<span style="color: #009900;">&#40;</span>SourcedCodeSiteSettingsConfig config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_string_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_string_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_string_array_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_string_array_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_long_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_long_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_number_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_number_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_short_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_short_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_char_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_char_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_byte_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_byte_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_double_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_double_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_float_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_float_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_boolean_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_boolean_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_password_getConfig_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_password_config_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">config_dropdown_example</span> <span style="color: #339933;">=</span> config.<span style="color: #006633;">config_dropdown_example</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="mention-block"><strong>Outcome Explained:</strong><br />
The outcome of the OSGI Service and Configuration will result in an editable configuration within the OSGI Apache Felix Console.<br />
<a class="foobox" href="https://sourcedcode.com/wp-content/uploads/2020/06/blog-post-attribute-definition-osgi-felix-done.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://sourcedcode.com/wp-content/uploads/2020/06/blog-post-attribute-definition-osgi-felix-done-150x150.png" alt="OSGI Apache Felix Console - Active Dialog - Result of Enablement of OSGI Configuration" width="150" height="75" /></a></div>
<hr class="spacer-large" />
<h2 id="inner-class">2. Inner Class &#8211; Defining OSGI Component&#8217;s Configuration with Configuration Interface</h2>
<p>You can also define the annotated @ObjectClassDefinition interface as an inner class as shown below. To use the inner class configuration interface within our OSGI service, we would need to annotation our service class with <span class="code-highlight-primary">@Designate</span>, with the ocd param pointing to the inner interface class. Next, it would be as simple as setting up the initial @Activate method.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.services.impl</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Activate</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.metatype.annotations.*</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> SourcedCodeSiteSettingsServiceInlineExample.<span style="color: #000000; font-weight: bold;">class</span>, immediate <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><br />
@Designate<span style="color: #009900;">&#40;</span>ocd <span style="color: #339933;">=</span> SourcedCodeSiteSettingsServiceInlineExample.<span style="color: #006633;">SourcedCodeSiteSettingsConfig</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SourcedCodeSiteSettingsServiceInlineExample <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @ObjectClassDefinition<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SourcedCode - Site Settings Configuration&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;dialog description&quot;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> @<span style="color: #000000; font-weight: bold;">interface</span> SourcedCodeSiteSettingsConfig <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> config_string_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;Default String&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String[] Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String[] Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> config_string_array_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;item1&quot;</span>, <span style="color: #0000ff;">&quot;item2&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; @AttributeDefinition<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Long Label&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Long Config Example Description&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">LONG</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">long</span> config_long_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> 0L<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Activate<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> activate<span style="color: #009900;">&#40;</span>SourcedCodeSiteSettingsConfig config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h3>Thoughts&#8230;</h3>
<p>I typically try my best to keep the configuration interface decoupled with OSGi components. This promotes better readability and maintainability. I would recommend you to stick with the Single Responsibility Principle; to separate concerns.</p>
<div class="mention-block"><strong>Note!</strong><br />
@AttributeDefinitions are the underlying formatting of how each OSGI configuration interaces are put together. If you wish to learn more about this topic, checkout this article: <a href="https://sourcedcode.com/blog/aem/osgi-r6-configuration-attributedefinition-essentials-reference-guide" target="_blank" rel="noopener noreferrer">OSGI R6 Configuration @AttributeDefinition Essentials Reference Guide</a>.</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/how-to-enable-configuration-for-osgi-components-with-r6">How to Enable Configuration for OSGI Components with R6</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/how-to-enable-configuration-for-osgi-components-with-r6/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>OSGI R6 Configuration @AttributeDefinition Essentials Reference Guide</title>
		<link>https://sourcedcode.com/blog/aem/osgi-r6-configuration-attributedefinition-essentials-reference-guide</link>
					<comments>https://sourcedcode.com/blog/aem/osgi-r6-configuration-attributedefinition-essentials-reference-guide#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 03 Jun 2020 17:52:00 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[OSGI]]></category>
		<category><![CDATA[Quick Reference]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=1643</guid>

					<description><![CDATA[<p>When defining any OSGI component&#8217;s configuration its standard practice to use Declarative Services; these configurations can be edited in the OSGI Apache Felix Console. This can be done by creating a class interface with the annotated @ObjectClassDefinition, which includes a determined list of rules as @AttributeDefinition items. The @AttributeDefinition defines the supporting configuration for a [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/osgi-r6-configuration-attributedefinition-essentials-reference-guide">OSGI R6 Configuration @AttributeDefinition Essentials Reference Guide</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>When defining any OSGI component&#8217;s configuration its standard practice to use Declarative Services; these configurations can be edited in the OSGI Apache Felix Console. This can be done by creating a class interface with the annotated @ObjectClassDefinition, which includes a determined list of rules as @AttributeDefinition items.</p>
<p>The @AttributeDefinition defines the supporting configuration for a given OSGI component&#8217;s configuration. It allows you to set data types and default values for each given configuration. When an admin user interacts with the Felix console, how the @AttributeDefinition is defined will determine the look and feel of each of the input fields are presented.</p>
<div class="mention-block">
<p><strong>Quick Links</strong></p>
<ol>
<li><a href="#essential-parameters-explained">@AttributeDefinition Essential Parameters Explained</a></li>
<li><a href="#attribute-types">Attribute Types &#8211; References</a></li>
<li><a href="https://sourcedcode.com/blog/aem/osgi-r6-configuration-code-examples" target="_blank" rel="noopener noreferrer"><span class="code-highlight-primary">How to Enable Configuration for OSGI Components</span></a></li>
</ol>
<hr />
<p><strong>Attribute Types &#8211; References</strong></p>
<ol>
<li><a href="#attribute-type-string">String Attribute Type</a></li>
<li><a href="#attribute-type-string-array">String[] Attribute Type</a></li>
<li><a href="#attribute-type-long">Long Attribute Type</a></li>
<li><a href="#attribute-type-int">int Attribute Type</a></li>
<li><a href="#attribute-type-short">Short Attribute Type</a></li>
<li><a href="#attribute-type-char">Char Attribute Type</a></li>
<li><a href="#attribute-type-byte">byte Attribute Type</a></li>
<li><a href="#attribute-type-double">Double Attribute Type</a></li>
<li><a href="#attribute-type-float">Float Attribute Type</a></li>
<li><a href="#attribute-type-boolean">Boolean Attribute Type</a></li>
<li><a href="#attribute-type-password">Password Attribute Type</a></li>
<li><a href="#attribute-type-options">Options Attribute Type</a></li>
</ol>
</div>
<hr class="spacer-large" />
<h2 id="essential-parameters-explained">@AttributeDefinition Essential Parameters Explained</h2>
<p>As we know, the most basic way to define a configuration will be to utilise the annotation @AttributeDefinition. Let&#8217;s explore the essential configurations.</p>
<p>The @AttributeDefinition annotation accepts parameters as such:</p>
<ul>
<li>line 2: <strong>name</strong>: defines the human read-able label</li>
<li>line 3: <strong>description</strong>: description for the attribute configuration.</li>
<li>line 4: <strong>type</strong>: definition of the attribute&#8217;s data type.</li>
<li>          line 6: <strong>&lt;T&gt;</strong>: definition of the attribute&#8217;s data type</li>
<li>          line 6: <strong>name_of_conf_reference</strong>: configuration&#8217;s reference id : name.of.conf.reference</li>
<li>          line 7: <strong>&lt;T&gt;[]</strong>: multifield (multi data set) is supported.
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><br />
description<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><br />
type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><br />
<span style="color: #009900;">&#125;</span><br />
name_of_conf_reference<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">// [] name_of_conf_reference_list() default &quot;&quot;;</span></div></td></tr></tbody></table></div>
<p><strong>Explained:</strong><br />
Illustrating how the label, description, and the configuration reference shows up on the OSGI Apache Felix console.<br />
<a class="foobox" href="https://sourcedcode.com/wp-content/uploads/2020/06/blog-post-attribute-definition-legend-explained.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://sourcedcode.com/wp-content/uploads/2020/06/blog-post-attribute-definition-legend-explained.png" alt="OSGI Apache Felix Console - visual description of whats being reflected by the code." width="300" /></a></p>
<hr class="spacer-extra-large" />
<h2 id="attribute-types">Attribute Types</h2>
<p>With the understanding of how the <a href="#essential-parameters-explained">@AttributeDefinition Essential Parameters are used</a>, we can go ahead to utilise and to familiarize ourselves with the supported essential data attribute types; most of the basic attributes are covered below:</p>
<ol>
<li><a href="#attribute-type-string">String Attribute Type</a></li>
<li><a href="#attribute-type-string-array">String[] Attribute Type</a></li>
<li><a href="#attribute-type-long">Long Attribute Type</a></li>
<li><a href="#attribute-type-int">int Attribute Type</a></li>
<li><a href="#attribute-type-short">Short Attribute Type</a></li>
<li><a href="#attribute-type-char">Char Attribute Type</a></li>
<li><a href="#attribute-type-byte">byte Attribute Type</a></li>
<li><a href="#attribute-type-float">Float Attribute Type</a></li>
<li><a href="#attribute-type-double">Double Attribute Type</a></li>
<li><a href="#attribute-type-boolean">Boolean Attribute Type</a></li>
<li><a href="#attribute-type-password">Password Attribute Type</a></li>
<li><a href="#attribute-type-options">Options Attribute Type</a></li>
</ol>
<p>&nbsp;</p>
<h4 id="attribute-type-string">1. String Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #003399;">String</span> config_string_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;Default String&quot;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-string-array">2. String[] Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String[] Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;String[] Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> config_string_array_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;item1&quot;</span>, <span style="color: #0000ff;">&quot;item2&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<div align="center"><strong>Explained:</strong><br />
Illustrating how the multi-fields shows up on the OSGI Felix console.<br />
<a class="foobox" href="https://sourcedcode.com/wp-content/uploads/2020/06/blog-post-attribute-definition-multi-field-example.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://sourcedcode.com/wp-content/uploads/2020/06/blog-post-attribute-definition-multi-field-example-150x150.png" alt="OSGI Apache Felix Console - Multi-Field Highlight Example" width="150" height="75" /></a></div>
<div class="mention-block"><strong>Note:</strong><br />
Configuration can be set as multifield can be enabled without much effort, simply change the attribute type to an list type, shown in the <a href="#attribute-type-string-array">#2. String[] Attribute Type example</a>.</div>
<hr class="spacer-large" />
<h4 id="attribute-type-long">3. Long Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Long Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Long Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">LONG</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">long</span> config_long_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> 0L<span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-int">4. int Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;int Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;innt Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">INTEGER</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">int</span> config_number_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-short">5. Short Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Short Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Short Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">SHORT</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">short</span> config_short_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-char">6. Char Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Char Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Char Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">CHARACTER</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">char</span> config_char_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-byte">7. Byte Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Byte Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Byte Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">BYTE</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">byte</span> config_byte_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<div class="mention-block"><strong>See Code Examples!</strong><br />
Click here to view <a href="https://sourcedcode.com/blog/aem/osgi-r6-configuration-code-examples">OSGI R6 Configuration Code Examples</a>.</div>
<h4 id="attribute-type-double">8. Double Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Double Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Double Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">DOUBLE</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">double</span> config_double_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-float">9. Float Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Float Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Float Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">FLOAT</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">float</span> config_float_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-boolean">10. Boolean Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Boolean Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Boolean Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">BOOLEAN</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000066; font-weight: bold;">boolean</span> config_boolean_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-password">11. Password Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Password Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Password Config Example Description&quot;</span>,<br />
type <span style="color: #339933;">=</span> AttributeType.<span style="color: #006633;">PASSWORD</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #003399;">String</span> config_password_config_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<h4 id="attribute-type-options">12. Options Attribute Type</h4>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@AttributeDefinition<span style="color: #009900;">&#40;</span><br />
name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Dropdown Label&quot;</span>,<br />
description <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Dropdown Config Example Description&quot;</span>,<br />
options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
@<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PRODUCTION&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PRODUCTION&quot;</span><span style="color: #009900;">&#41;</span>,<br />
@<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;STAGING&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;STAGING&quot;</span><span style="color: #009900;">&#41;</span>,<br />
@<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UAT&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UAT&quot;</span><span style="color: #009900;">&#41;</span>,<br />
@<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;QA&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;QA&quot;</span><span style="color: #009900;">&#41;</span>,<br />
@<span style="color: #003399;">Option</span><span style="color: #009900;">&#40;</span>label <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DEVELOP&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DEVELOP&quot;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #003399;">String</span> config_dropdown_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0000ff;">&quot;DEVELOP&quot;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<div class="mention-block"><strong>See Code Examples!</strong><br />
Click here to view <a href="https://sourcedcode.com/blog/aem/osgi-r6-configuration-code-examples">OSGI R6 Configuration Code Examples</a>.</div>
</li>
</ul>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/osgi-r6-configuration-attributedefinition-essentials-reference-guide">OSGI R6 Configuration @AttributeDefinition Essentials Reference Guide</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/osgi-r6-configuration-attributedefinition-essentials-reference-guide/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
