001/** 002 * 003 * Copyright 2019 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jxmpp; 018 019import org.jxmpp.stringprep.XmppStringprep; 020import org.jxmpp.stringprep.simple.SimpleXmppStringprep; 021import org.jxmpp.util.Objects; 022 023/** 024 * A context with describes the configuration used by JXMPP. 025 */ 026public final class JxmppContext { 027 028 private static JxmppContext defaultContext; 029 030 /** 031 * Get the default context. 032 * 033 * @return the default context. 034 */ 035 public static JxmppContext getDefaultContext() { 036 return defaultContext; 037 } 038 039 private static XmppStringprep defaultXmppStringprep; 040 041 static { 042 SimpleXmppStringprep.setup(); 043 } 044 045 /** 046 * Set the default XmppStringprep interface. 047 * 048 * @param defaultXmppStringprep the default XmppStringprep interface. 049 */ 050 public static void setDefaultXmppStringprep(XmppStringprep defaultXmppStringprep) { 051 JxmppContext.defaultXmppStringprep = Objects.requireNonNull(defaultXmppStringprep, "defaultXmppStringprep"); 052 updateDefaultContext(); 053 } 054 055 private static void updateDefaultContext() { 056 defaultContext = builder() 057 .enableCaching() 058 .withXmppStringprep(defaultXmppStringprep) 059 .build(); 060 } 061 062 private final boolean cachingEnabled; 063 064 public final XmppStringprep xmppStringprep; 065 066 private JxmppContext(Builder builder) { 067 cachingEnabled = builder.cachingEnabled; 068 xmppStringprep = Objects.requireNonNull(builder.xmppStringprep, "xmppStringprep"); 069 } 070 071 /** 072 * Returns true if String, Part and Jid caching is enabled. 073 * 074 * @return true if caching is enabled. 075 */ 076 public boolean isCachingEnabled() { 077 return cachingEnabled; 078 } 079 080 /** 081 * Construct and retrieve a new builder. 082 * 083 * @return a new builder. 084 */ 085 public static Builder builder() { 086 return new Builder(); 087 } 088 089 public static class Builder { 090 private boolean cachingEnabled; 091 092 private XmppStringprep xmppStringprep; 093 094 /** 095 * Enable String, Part and Jid caching. 096 * 097 * @return a reference to this builder. 098 */ 099 public Builder enableCaching() { 100 cachingEnabled = true; 101 return this; 102 } 103 104 /** 105 * Set the used XmppStringprep. 106 * 107 * @param xmppStringprep the XmppStringprep to use. 108 * @return a reference to this builder. 109 */ 110 public Builder withXmppStringprep(XmppStringprep xmppStringprep) { 111 this.xmppStringprep = Objects.requireNonNull(xmppStringprep, "xmppStringprep"); 112 return this; 113 } 114 115 /** 116 * Build a JxmppContext. 117 * 118 * @return a newly build JxmppContext. 119 */ 120 public JxmppContext build() { 121 return new JxmppContext(this); 122 } 123 } 124}