2 * Copyright (c) 2013 Robert Varga. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.bgpcep.tcpmd5.jni;
10 import java.io.IOException;
11 import java.nio.channels.Channel;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import com.google.common.base.Preconditions;
19 * Implementation of KeyAccess using Java Native Interface plugin to talk
20 * directly to the underlying operating system.
22 public final class NativeKeyAccess implements KeyAccess {
23 private static final Logger LOG = LoggerFactory.getLogger(NativeKeyAccess.class);
26 NarSystem.loadLibrary();
28 int rt = NarSystem.runUnitTests();
30 LOG.warn("Run-time initialization failed");
32 LOG.debug("Run-time found {} supported channel classes", rt);
36 private static native boolean isClassSupported0(Class<?> channel);
37 private static native byte[] getChannelKey0(Channel channel) throws IOException;
38 private static native void setChannelKey0(Channel channel, byte[] key) throws IOException;
40 private final Channel channel;
42 private NativeKeyAccess(final Channel channel) {
43 this.channel = Preconditions.checkNotNull(channel);
46 public static KeyAccess create(final Channel channel) {
47 if (isClassSupported0(channel.getClass())) {
48 return new NativeKeyAccess(channel);
54 public static boolean isAvailableForClass(final Class<?> clazz) {
55 return isClassSupported0(clazz);
59 public byte[] getKey() throws IOException {
60 synchronized (channel) {
61 return getChannelKey0(channel);
66 public void setKey(final byte[] key) throws IOException {
67 synchronized (channel) {
68 setChannelKey0(channel, Preconditions.checkNotNull(key));