Skip to content

Commit

Permalink
Encode client version into Java source file instead of pulsar-client-…
Browse files Browse the repository at this point in the history
…version.properties (apache#3880)
  • Loading branch information
merlimat authored Mar 24, 2019
1 parent 16e3b12 commit 5653df8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 59 deletions.
13 changes: 13 additions & 0 deletions pulsar-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filtering-java-templates</id>
<goals>
<goal>filter-sources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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
* "License"); 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
* "AS IS" 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.
*/
package org.apache.pulsar;

public class PulsarVersion {
public static String getVersion() {
return "${project.version}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static org.apache.pulsar.client.impl.HttpClient.getPulsarClientVersion;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
Expand All @@ -40,6 +38,7 @@

import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.pulsar.PulsarVersion;
import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.AuthenticationDataProvider;
import org.apache.pulsar.client.api.PulsarClientException;
Expand Down Expand Up @@ -202,7 +201,7 @@ protected ByteBuf newConnectCommand() throws Exception {
authenticationDataProvider = authentication.getAuthData(remoteHostName);
AuthData authData = authenticationDataProvider.authenticate(AuthData.of(AuthData.INIT_AUTH_DATA));
return Commands.newConnect(authentication.getAuthMethodName(), authData, this.protocolVersion,
getPulsarClientVersion(), proxyToTargetBrokerAddress, null, null, null);
PulsarVersion.getVersion(), proxyToTargetBrokerAddress, null, null, null);
}

@Override
Expand Down Expand Up @@ -300,7 +299,7 @@ protected void handleAuthChallenge(CommandAuthChallenge authChallenge) {
ByteBuf request = Commands.newAuthResponse(authentication.getAuthMethodName(),
authData,
this.protocolVersion,
getPulsarClientVersion());
PulsarVersion.getVersion());

if (log.isDebugEnabled()) {
log.debug("{} Mutual auth {}", ctx.channel(), authentication.getAuthMethodName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Properties;
import java.util.concurrent.CompletableFuture;

import org.apache.pulsar.PulsarVersion;
import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.AuthenticationDataProvider;
import org.apache.pulsar.client.api.PulsarClientException;
Expand Down Expand Up @@ -80,7 +81,7 @@ protected HttpClient(String serviceUrl, Authentication authentication,
confBuilder.setFollowRedirect(true);
confBuilder.setConnectTimeout(connectTimeoutInSeconds * 1000);
confBuilder.setReadTimeout(readTimeoutInSeconds * 1000);
confBuilder.setUserAgent(String.format("Pulsar-Java-v%s", getPulsarClientVersion()));
confBuilder.setUserAgent(String.format("Pulsar-Java-v%s", PulsarVersion.getVersion()));
confBuilder.setKeepAliveStrategy(new DefaultKeepAliveStrategy() {
@Override
public boolean keepAlive(Request ahcRequest, HttpRequest request, HttpResponse response) {
Expand Down Expand Up @@ -193,34 +194,5 @@ public void onThrowable(Throwable t) {

}

/**
* Looks for a file called pulsar-client-version.properties and returns the client version
*
* @return client version or unknown version depending on whether the file is found or not.
*/
public static String getPulsarClientVersion() {
String path = "/pulsar-client-version.properties";
String unknownClientIdentifier = "UnknownClient";

try {
InputStream stream = HttpClient.class.getResourceAsStream(path);
if (stream == null) {
return unknownClientIdentifier;
}
Properties props = new Properties();
try {
props.load(stream);
String version = (String) props.get("pulsar-client-version");
return version;
} catch (IOException e) {
return unknownClientIdentifier;
} finally {
stream.close();
}
} catch (Throwable t) {
return unknownClientIdentifier;
}
}

private static final Logger log = LoggerFactory.getLogger(HttpClient.class);
}
20 changes: 0 additions & 20 deletions pulsar-client/src/main/resources/pulsar-client-version.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@
*/
package org.apache.pulsar.proxy.server;

import static org.apache.pulsar.client.impl.HttpClient.getPulsarClientVersion;
import io.netty.buffer.ByteBuf;
import io.netty.channel.EventLoopGroup;

import org.apache.pulsar.PulsarVersion;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.impl.ClientCnx;
import org.apache.pulsar.client.impl.conf.ClientConfigurationData;
import org.apache.pulsar.common.api.Commands;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.channel.EventLoopGroup;

public class ProxyClientCnx extends ClientCnx {

String clientAuthRole;
Expand Down Expand Up @@ -58,7 +57,8 @@ protected ByteBuf newConnectCommand() throws PulsarClientException {
authData = authentication.getAuthData().getCommandData();
}
return Commands.newConnect(authentication.getAuthMethodName(), authData, protocolVersion,
getPulsarClientVersion(), proxyToTargetBrokerAddress, clientAuthRole, clientAuthData, clientAuthMethod);
PulsarVersion.getVersion(), proxyToTargetBrokerAddress, clientAuthRole, clientAuthData,
clientAuthMethod);
}

private static final Logger log = LoggerFactory.getLogger(ProxyClientCnx.class);
Expand Down

0 comments on commit 5653df8

Please sign in to comment.