diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/BaseKubernetesClientBuilder.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/BaseKubernetesClientBuilder.java new file mode 100644 index 00000000000..5e280c3792f --- /dev/null +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/BaseKubernetesClientBuilder.java @@ -0,0 +1,75 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed 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 io.fabric8.kubernetes.client; + +import io.fabric8.kubernetes.client.http.HttpClient; +import io.fabric8.kubernetes.client.utils.Serialization; + +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; + +public class BaseKubernetesClientBuilder> { + + private Config config; + private HttpClient.Factory factory; + private Class clazz; + + protected BaseKubernetesClientBuilder(String className) { + try { + this.clazz = (Class) Class.forName(className); + } catch (ClassNotFoundException e) { + throw KubernetesClientException.launderThrowable(e); + } + } + + public KubernetesClient build() { + if (config == null) { + config = new ConfigBuilder().build(); + } + try { + if (factory == null) { + return clazz.getConstructor(Config.class).newInstance(config); + } + HttpClient client = factory.createHttpClient(config); + return clazz.getConstructor(HttpClient.class, Config.class).newInstance(client, config); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | SecurityException e) { + throw KubernetesClientException.launderThrowable(e); + } + } + + public BaseKubernetesClientBuilder withConfig(Config config) { + this.config = config; + return this; + } + + public BaseKubernetesClientBuilder withConfig(String config) { + this.config = Serialization.unmarshal(config); + return this; + } + + public BaseKubernetesClientBuilder withConfig(InputStream config) { + this.config = Serialization.unmarshal(config); + return this; + } + + public BaseKubernetesClientBuilder withHttpClientFactory(HttpClient.Factory factory) { + this.factory = factory; + return this; + } + +} diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClientBuilder.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClientBuilder.java new file mode 100644 index 00000000000..dafd3aa1f9d --- /dev/null +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClientBuilder.java @@ -0,0 +1,25 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed 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 io.fabric8.kubernetes.client; + +public class KubernetesClientBuilder extends BaseKubernetesClientBuilder { + + public KubernetesClientBuilder() { + super("io.fabric8.kubernetes.client.DefaultKubernetesClient"); + } + +} diff --git a/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftClientBuilder.java b/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftClientBuilder.java new file mode 100644 index 00000000000..7991dc76b14 --- /dev/null +++ b/openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftClientBuilder.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed 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 io.fabric8.openshift.client; + +import io.fabric8.kubernetes.client.BaseKubernetesClientBuilder; + +public class OpenShiftClientBuilder extends BaseKubernetesClientBuilder { + + public OpenShiftClientBuilder() { + super("io.fabric8.openshift.client.DefaultOpenShiftClient"); + } + + public OpenShiftClientBuilder withOpenShiftConfig(OpenShiftConfig config) { + super.withConfig(config); + return this; + } + + @Override + public OpenShiftClient build() { + return (OpenShiftClient)super.build(); + } + +}