blob: 3ab3ace693d4adfeb4ed78e88bb0519d5e38d494 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>TurbineUniqueIdService.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Turbine</a> &gt; <a href="index.source.html" class="el_package">org.apache.turbine.services.uniqueid</a> &gt; <span class="el_source">TurbineUniqueIdService.java</span></div><h1>TurbineUniqueIdService.java</h1><pre class="source lang-java linenums">package org.apache.turbine.services.uniqueid;
import java.nio.charset.StandardCharsets;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.security.MessageDigest;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.turbine.Turbine;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.util.GenerateUniqueId;
/**
* &lt;p&gt; This is an implementation of {@link UniqueIdService}.
*
* @author &lt;a href=&quot;mailto:Rafal.Krzewski@e-point.pl&quot;&gt;Rafal Krzewski&lt;/a&gt;
* @author &lt;a href=&quot;mailto:hps@intermeta.de&quot;&gt;Henning P. Schmiedehausen&lt;/a&gt;
* @version $Id$
*/
<span class="fc" id="L44">public class TurbineUniqueIdService</span>
extends TurbineBaseService
implements UniqueIdService
{
/** Logging */
<span class="fc" id="L49"> private static final Logger log = LogManager.getLogger(TurbineUniqueIdService.class);</span>
/** The identifier of this instance of turbine. */
<span class="fc" id="L52"> private static String turbineId = &quot;UNKNOWN&quot;;</span>
<span class="fc" id="L54"> private static String turbineURL = &quot;UNKNOWN&quot;;</span>
<span class="fc" id="L56"> private static final AtomicInteger counter = new AtomicInteger();</span>
/**
* &lt;p&gt; Initializes the service upon first Turbine.doGet()
* invocation.
*/
@Override
public void init()
throws InitializationException
{
try
{
<span class="nc" id="L69"> counter.set(0);</span>
// This might be a problem if the unique Id Service runs
// before Turbine got its first request. In this case,
// getDefaultServerData will return just a dummy value
// which is the same for all instances of Turbine.
//
// TODO This needs definitely further working.
<span class="nc" id="L77"> turbineURL = Turbine.getDefaultServerData().toString();</span>
<span class="nc" id="L79"> MessageDigest md = MessageDigest.getInstance(&quot;MD5&quot;);</span>
<span class="nc" id="L80"> byte [] bytesId = md.digest(turbineURL.getBytes(StandardCharsets.UTF_8));</span>
<span class="nc" id="L81"> turbineId = new String(Base64.encodeBase64(bytesId),</span>
StandardCharsets.UTF_8);
<span class="nc" id="L84"> log.info(&quot;This is Turbine instance running at: {}&quot;, turbineURL);</span>
<span class="nc" id="L85"> log.info(&quot;The instance id is #{}&quot;, turbineId);</span>
<span class="nc" id="L86"> setInit(true);</span>
}
<span class="nc" id="L88"> catch (Exception e)</span>
{
<span class="nc" id="L90"> throw new InitializationException(</span>
&quot;Could not initialize TurbineUniqueId Service&quot;, e);
<span class="nc" id="L92"> }</span>
<span class="nc" id="L93"> }</span>
/**
* &lt;p&gt; Writes a message to the log upon system shutdown.
*/
@Override
public void shutdown()
{
<span class="nc" id="L101"> log.info(&quot;Turbine instance running at {} shutting down.&quot;, turbineURL);</span>
<span class="nc" id="L102"> }</span>
/**
* &lt;p&gt; Returns an identifier of this Turbine instance that is unique
* both on the server and worldwide. This identifier is computed
* as an MD5 sum of the URL (including schema, address, port if
* different that 80/443 respectively, context and servlet name).
* There is an overwhelming probability that this id will be
* different that all other Turbine instances online.
*
* @return A String with the instance identifier.
*/
@Override
public String getInstanceId()
{
<span class="nc" id="L117"> return turbineId;</span>
}
/**
* &lt;p&gt; Returns an identifier that is unique within this turbine
* instance, but does not have random-like appearance.
*
* @return A String with the non-random looking instance
* identifier.
*/
@Override
public String getUniqueId()
{
<span class="nc" id="L130"> int current = counter.getAndIncrement();</span>
<span class="nc" id="L131"> String id = Integer.toString(current);</span>
// If you manage to get more than 100 million of ids, you'll
// start getting ids longer than 8 characters.
<span class="nc bnc" id="L135" title="All 2 branches missed."> if (current &lt; 100000000)</span>
{
<span class="nc" id="L137"> id = (&quot;00000000&quot; + id).substring(id.length());</span>
}
<span class="nc" id="L139"> return id;</span>
}
/**
* &lt;p&gt; Returns a unique identifier that looks like random data.
*
* @return A String with the random looking instance identifier.
*/
@Override
public String getPseudorandomId()
{
<span class="nc" id="L150"> return GenerateUniqueId.getIdentifier();</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>