Search in sources :

Example 66 with Element

use of net.sf.ehcache.Element in project sling by apache.

the class CacheImpl method values.

/**
	 * {@inheritDoc}
	 * 
	 * @see org.apache.sling.commons.cache.api.Cache#list()
	 */
@SuppressWarnings("unchecked")
public Collection<V> values() {
    List<String> keys = cache.getKeys();
    List<V> values = new ArrayList<V>();
    for (String k : keys) {
        Element e = cache.get(k);
        if (e != null) {
            values.add((V) e.getObjectValue());
        }
    }
    return values;
}
Also used : Element(net.sf.ehcache.Element) ArrayList(java.util.ArrayList)

Example 67 with Element

use of net.sf.ehcache.Element in project sling by apache.

the class CacheImpl method put.

/**
	 * {@inherit-doc}
	 * 
	 * @see org.apache.sling.commons.cache.api.Cache#put(java.lang.String,
	 *      java.lang.Object)
	 */
@SuppressWarnings("unchecked")
public V put(String key, V payload) {
    V previous = null;
    if (cache.isKeyInCache(key)) {
        Element e = cache.get(key);
        if (e != null) {
            previous = (V) e.getObjectValue();
        }
    }
    cache.put(new Element(key, payload));
    return previous;
}
Also used : Element(net.sf.ehcache.Element)

Example 68 with Element

use of net.sf.ehcache.Element in project cas by apereo.

the class CRLDistributionPointRevocationChecker method getCRLs.

@Override
protected List<X509CRL> getCRLs(final X509Certificate cert) {
    if (this.crlCache == null) {
        throw new IllegalArgumentException("CRL cache is not defined");
    }
    if (this.fetcher == null) {
        throw new IllegalArgumentException("CRL fetcher is not defined");
    }
    if (getExpiredCRLPolicy() == null) {
        throw new IllegalArgumentException("Expiration CRL policy is not defined");
    }
    if (getUnavailableCRLPolicy() == null) {
        throw new IllegalArgumentException("Unavailable CRL policy is not defined");
    }
    final URI[] urls = getDistributionPoints(cert);
    LOGGER.debug("Distribution points for [{}]: [{}].", CertUtils.toString(cert), Arrays.asList(urls));
    final List<X509CRL> listOfLocations = new ArrayList<>(urls.length);
    boolean stopFetching = false;
    try {
        for (int index = 0; !stopFetching && index < urls.length; index++) {
            final URI url = urls[index];
            final Element item = this.crlCache.get(url);
            if (item != null) {
                LOGGER.debug("Found CRL in cache for [{}]", CertUtils.toString(cert));
                final byte[] encodedCrl = (byte[]) item.getObjectValue();
                final X509CRL crlFetched = this.fetcher.fetch(new ByteArrayResource(encodedCrl));
                if (crlFetched != null) {
                    listOfLocations.add(crlFetched);
                } else {
                    LOGGER.warn("Could fetch X509 CRL for [{}]. Returned value is null", url);
                }
            } else {
                LOGGER.debug("CRL for [{}] is not cached. Fetching and caching...", CertUtils.toString(cert));
                try {
                    final X509CRL crl = this.fetcher.fetch(url);
                    if (crl != null) {
                        LOGGER.info("Success. Caching fetched CRL at [{}].", url);
                        addCRL(url, crl);
                        listOfLocations.add(crl);
                    }
                } catch (final Exception e) {
                    LOGGER.error("Error fetching CRL at [{}]", url, e);
                    if (this.throwOnFetchFailure) {
                        throw Throwables.propagate(e);
                    }
                }
            }
            if (!this.checkAll && !listOfLocations.isEmpty()) {
                LOGGER.debug("CRL fetching is configured to not check all locations.");
                stopFetching = true;
            }
        }
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
    LOGGER.debug("Found [{}] CRLs", listOfLocations.size());
    return listOfLocations;
}
Also used : X509CRL(java.security.cert.X509CRL) Element(net.sf.ehcache.Element) ArrayList(java.util.ArrayList) ByteArrayResource(org.springframework.core.io.ByteArrayResource) URI(java.net.URI) DistributionPoint(org.bouncycastle.asn1.x509.DistributionPoint) MalformedURLException(java.net.MalformedURLException)

Example 69 with Element

use of net.sf.ehcache.Element in project cas by apereo.

the class EhCacheTicketRegistry method getTicket.

@Override
public Ticket getTicket(final String ticketIdToGet) {
    final String ticketId = encodeTicketId(ticketIdToGet);
    if (ticketId == null) {
        return null;
    }
    final Element element = this.ehcacheTicketsCache.get(ticketId);
    if (element == null) {
        LOGGER.debug("No ticket by id [{}] is found in the registry", ticketId);
        return null;
    }
    final Ticket ticket = decodeTicket((Ticket) element.getObjectValue());
    final CacheConfiguration config = new CacheConfiguration();
    config.setTimeToIdleSeconds(ticket.getExpirationPolicy().getTimeToIdle());
    config.setTimeToLiveSeconds(ticket.getExpirationPolicy().getTimeToLive());
    if (element.isExpired(config) || ticket.isExpired()) {
        LOGGER.debug("Ticket [{}] has expired", ticket.getId());
        this.ehcacheTicketsCache.evictExpiredElements();
        this.ehcacheTicketsCache.flush();
        return null;
    }
    return ticket;
}
Also used : Ticket(org.apereo.cas.ticket.Ticket) Element(net.sf.ehcache.Element) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration)

Example 70 with Element

use of net.sf.ehcache.Element in project cas by apereo.

the class EhCacheTicketRegistry method addTicket.

@Override
public void addTicket(final Ticket ticketToAdd) {
    final Ticket ticket = encodeTicket(ticketToAdd);
    final Element element = new Element(ticket.getId(), ticket);
    int idleValue = ticketToAdd.getExpirationPolicy().getTimeToIdle().intValue();
    if (idleValue <= 0) {
        idleValue = ticketToAdd.getExpirationPolicy().getTimeToLive().intValue();
    }
    if (idleValue <= 0) {
        idleValue = Integer.MAX_VALUE;
    }
    element.setTimeToIdle(idleValue);
    int aliveValue = ticketToAdd.getExpirationPolicy().getTimeToLive().intValue();
    if (aliveValue <= 0) {
        aliveValue = Integer.MAX_VALUE;
    }
    element.setTimeToLive(aliveValue);
    LOGGER.debug("Adding ticket [{}] to the cache [{}] to live [{}] seconds and stay idle for [{}] seconds", ticket.getId(), this.ehcacheTicketsCache.getName(), aliveValue, idleValue);
    this.ehcacheTicketsCache.put(element);
}
Also used : Ticket(org.apereo.cas.ticket.Ticket) Element(net.sf.ehcache.Element)

Aggregations

Element (net.sf.ehcache.Element)114 Test (org.junit.Test)21 CacheKey (org.apereo.portal.utils.cache.CacheKey)8 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 Cache (net.sf.ehcache.Cache)7 HashSet (java.util.HashSet)6 CacheException (net.sf.ehcache.CacheException)6 MalformedURLException (java.net.MalformedURLException)5 ConfigurationException (javax.naming.ConfigurationException)5 Ehcache (net.sf.ehcache.Ehcache)5 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)5 EntityIdentifier (org.apereo.portal.EntityIdentifier)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URISyntaxException (java.net.URISyntaxException)4 SQLException (java.sql.SQLException)4 EntityExistsException (javax.persistence.EntityExistsException)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 BaseCacheTest (org.apache.camel.component.BaseCacheTest)4